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 33,078 of 33,346   
   space.cadet@nokiamail.com to All   
   Is a lambda copied by value when used in   
   22 May 13 22:39:48   
   
   { Please limit your text to fit within 80 columns, preferably around 70,   
     so that readers don't have to scroll horizontally to read each line.   
     This article has been reformatted manually by the moderator. -mod }   
      
   Hi,   
   When a lambda is used with say, a std::find_if algorithm, is it a good   
   idea to allow it to change some variable in the object?   
      
   That is, is the following a good practice?   
   std::find_if(std::begin(collection), std::end(collection), [&](thing t)   
   {   
     if (t == something)   
     {   
       return true;   
     }   
     ++row_number;   
     return false;   
   });   
      
   // perform some action based upon the row_number   
      
   I know that changing state in function predicates is considered a bad   
   idea because they may be copied by value and the state may, therefore,   
   be changed more than once which may lead to unexpected results.   
      
   Do the same considerations apply to lambdas as well? Doesn't the   
   compiler create a function out of lambda internally?   
      
   Thanks   
      
      
   --   
         [ 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