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,082 of 33,346   
   =?windows-1252?Q?Daniel_Kr=FCgler?= to space.cadet@nokiamail.com   
   Re: Is a lambda copied by value when use   
   23 May 13 04:18:11   
   
   From: daniel.kruegler@googlemail.com   
      
   On 2013-05-23 07:39, space.cadet@nokiamail.com wrote:   
   > 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?   
      
   As usual you have to know what you are doing ;-)   
      
   > 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;   
   > });   
      
   Note that stateful function objects in Library algorithms have to   
   ensure that they keep the state external, because such library   
   function are free to copy the provided function object. This is also   
   indicated by the non-normative note in 25.1 p10:   
      
   "[ Note: Unless otherwise specified, algorithms that take function   
   objects as arguments are permitted to copy those function objects   
   freely. Programmers for whom object identity is important should   
   consider using a wrapper class that points to a noncopied   
   implementation object such as reference_wrapper (20.10.3), or some   
   equivalent solution. —end note ]"   
      
   > // 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?   
      
   In principle the same considerations apply to lambda objects. But if   
   you capture by reference, this has basically the same effect as if you   
   would write your own function object that uses a reference to the   
   externally kept state. I don't see how the specification of lambda   
   closures could be read different from keeping the identical reference   
   to the captured entities in all copies. In other words: If I   
   understand your example correctly, the reference to row_number is   
   always to the same object, irrespective of the number of copies of the   
   lambda closure.   
      
   I'm not really sure that I understand your question that refers to   
   "create a function out of lambda internally". What are you trying to   
   say here?   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
      
      
   --   
         [ 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