Prev: Template expansion during function template matching?
Next: Implicit conversions for value subtyping
From: Mathias Gaunard on 21 Apr 2010 04:35 On Apr 21, 7:47 am, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote: > I mean, I would never define a function like this, returning a reference to a local variable: > > std::string& badFnc() > { > std::string s( "Hello" ); > return s; > > } > > But will it be ok with lambdas? > > doSomeWork( > someArg, > [&]() -> std::string& { std::string s( "Hello" ); return s; } ); > > I.e. will s survive until doSomeWork() is done? Why would a lambda be any different? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jens Schmidt on 24 Apr 2010 03:59 Martin B. wrote: > DeMarcus wrote: >> (....) >> >> I'm going to use lambdas as lazy evaluators that are only run if a >> certain criteria is fulfilled. Maybe I wished (before I realized the >> danger) that I could use the lambda just like a temporary, i.e. >> >> doSomeWork( someArg, std::string( "Hello" ) ); >> >> ...would become... >> >> doSomeWork( >> someArg, >> [&]() -> std::string& { return std::string( "Hello" ); } ); >> >> // (no string copy) >> > > While this probably is compiler specific, in such an example there > shouldn't be any copy anyway because of RVO. > (and RVO - on Visual Studio - is not even an optimizer setting, it's > even done with all optimizations turned off). Is the string copy really the thing possibly saved by lazy evaluation? At first reading I got the impression that the critical part was if the constructor for string was used always or just if needed. Of cause the string constructor is quite cheap, but there may be much more complex code in the lambda body. -- Greetings, Jens Schmidt [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin B. on 25 Apr 2010 21:41 Jens Schmidt wrote: > Martin B. wrote: > >> DeMarcus wrote: >>> (....) >>> >>> I'm going to use lambdas as lazy evaluators that are only run if a >>> certain criteria is fulfilled. Maybe I wished (before I realized the >>> danger) that I could use the lambda just like a temporary, i.e. >>> >>> doSomeWork( someArg, std::string( "Hello" ) ); >>> >>> ...would become... >>> >>> doSomeWork( >>> someArg, >>> [&]() -> std::string& { return std::string( "Hello" ); } ); >>> >>> // (no string copy) >>> >> While this probably is compiler specific, in such an example there >> shouldn't be any copy anyway because of RVO. >> (and RVO - on Visual Studio - is not even an optimizer setting, it's >> even done with all optimizations turned off). > > Is the string copy really the thing possibly saved by lazy evaluation? > At first reading I got the impression that the critical part was > if the constructor for string was used always or just if needed. > Of cause the string constructor is quite cheap, but there may be much > more complex code in the lambda body. I understood the OP this way: 1) Want to use lazy evaluation 2) Will use lambda for that 3) While I'm at it, it would be nice if I could return a ref-to-temporary from the lamba, since that will save me a copy I commented on (3) about the copy. The lazy evaluation should still work regardless. br, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Pages: 1 2 Prev: Template expansion during function template matching? Next: Implicit conversions for value subtyping |