home bbs files messages ]

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 31,612 of 33,346   
   Dave Abrahams to All   
   Re: "for_each + lambda" vs "ranged for"   
   03 Nov 11 16:54:09   
   
   4a46b0b4   
   From: dave@boostpro.com   
      
   on Thu Nov 03 2011, topoden  wrote:   
      
   > On Nov 2, 2:19 pm, Dave Abrahams  wrote:   
   >> Consider break, continue, goto, the ability to remember the   
   >> value of the last item seen, etc.   
   >>   
   >> On the other hand, for_each may perform library-based optimizations such   
   >> as loop unrolling and segmented traversal.   
   >>   
   >> They each have their own strengths.  I'd say range-based for loops are   
   >> more flexible, while std::for_each is potentially more efficient.   
   >   
   > I understand "break, continue, goto" reasons. But can not we have "the   
   > ability to remember the value of the last item seen" with   
   > std::for_each + lambda + capture by ref? Say something like this:   
   >   
   > boost::optional prev_element;   
   > std::for_each(v.begin(), v.end(), [&prev_element] (int n)   
   > {   
   >    ...   
   >   
   >    prev_element = n;   
   > });   
      
   Yes, that works.   
   --   
   Dave Abrahams   
   BoostPro Computing   
   http://www.boostpro.com   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca