Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,416 of 33,346    |
|    Dave Harris to Michael Kilburn    |
|    Re: Will we ever be able to throw from a    |
|    18 Jun 12 13:49:03    |
   
   839d3781   
   From: brangdon@cix.compulink.co.uk   
      
   crusader.mike@gmail.com (Michael Kilburn) wrote (abridged):   
   > So, In my example (with db connection/etc) you propose to move scope   
   > of life for every object (that needs a shutdown call) with a   
   > function?   
      
   It's often a good idea for each function to do one thing, rather than three   
   different things (open, use, close).   
      
      
   > This is definitely no going to fly when there are multiple objects   
   > like this -- N objects means N nested functions, if function at the   
   > bottom needs access to a variable living in topmost scope -- are you   
   > going pass a reference to it through entire chain of function calls?   
      
   In C++11 we can use lambda expressions (as I mentioned a while back).   
   Or are they what you mean by "poorly written Lisp code"?   
      
      
   > void save()   
   > {   
   > int k = ...;   
   > MyClass m = ...;   
   >   
   > connection c(/* ctor args */);   
   >   
   > {   
   > TemporaryTable t1("XXX", c); // drops table in dtor   
   > ...   
   > Transaction tran(c);   
   >   
   > ...   
   > m.do_smth();   
   >   
   > // better, but do you really expect user to do this?   
   > if (...) { ...; tran.shutdown(); t1.shutdown();   
   > c.shutdown();   
   > return; }   
   > k = ...;   
   >   
   > TemporaryTable t2("YYY", c); // drops table in dtor   
   > ...   
   > if (...) { ...; t2.shutdown(); tran.shutdown();   
   > t1.shutdown(); c.shutdown(); return; }   
   >   
   > }   
   > c.shutdown();   
   > }   
      
   void save() {   
    int k = ...;   
    MyClass m = ...;   
      
    connection c(/* ctor args */);   
    c.open( [&] {   
    TemporaryTable t1("XXX", c);   
    t1.open( [&] {   
    Transaction tran(c);   
    tran.open( [&] {   
    m.do_smth();   
    if (...)   
    return;   
    k = ...;   
      
    TemporaryTable t2("YYY", c);   
    t2.open( [&] {   
    ...   
    if (...)   
    return;   
    } );   
    } );   
    } );   
    } );   
   }   
      
   Or somesuch. Where each "open" is like:   
      
    void Transaction::open( const std::function
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca