Prev: Template expansion during function template matching?
Next: Implicit conversions for value subtyping
From: DeMarcus on 18 Apr 2010 21:04 Hi, In �12.2/5 the lifetime of temporaries is defined. However, what's the lifetime of SomeClass in below example? doSomeWork( someArg, [&]{ return SomeClass(); } ); Is the lifetime of SomeClass until the return statement or the end of the full-expression doSomeWork()? Thanks, Daniel -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin B. on 22 Apr 2010 08:20 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). Not sure if lambdas are any different here than "normal" functions. I'd guess not? br, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: DeMarcus on 20 Apr 2010 15:47 DeMarcus wrote: > Hi, > > In �12.2/5 the lifetime of temporaries is defined. However, what's the > lifetime of SomeClass in below example? > > doSomeWork( someArg, [&]{ return SomeClass(); } ); > > Is the lifetime of SomeClass until the return statement or the end of > the full-expression doSomeWork()? > > Thanks, > Daniel > 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? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: DeMarcus on 21 Apr 2010 22:08 Mathias Gaunard wrote: > 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? > > I may ask irrational questions now and then. Most often they seem silly, but once in a while I find them legitimate. With sound reasoning a lambda of course should be like a normal function the way Daniel Kr�gler explained, however, what I like with lambdas is that you provide it with your whole stack via [&] and I thought there may be more nice surprises. 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) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 21 Apr 2010 04:33 On 21 Apr., 08:47, DeMarcus <use_my_alias_h...(a)hotmail.com> wrote: > DeMarcus wrote: > > Hi, > > > In �12.2/5 the lifetime of temporaries is defined. However, what's the > > lifetime of SomeClass in below example? > > > doSomeWork( someArg, [&]{ return SomeClass(); } ); > > > Is the lifetime of SomeClass until the return statement or the end of > > the full-expression doSomeWork()? > > > Thanks, > > Daniel > > 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? No. > doSomeWork( > someArg, > [&]() -> std::string& { std::string s( "Hello" ); return s; } ); > > I.e. will s survive until doSomeWork() is done? No. Lambda-expressions define the semantics of their /compound-statement/ as that of the function body of a function call operator (see 5.1.2 [expr.prim.lambda]/7), therefore you have to apply the same rules as for other functions. More specifically, the /compound-statement/ in your example is: { std::string s( "Hello" ); return s; } If s would be a captured entity, extra rules would apply that are described within sub-clause 5.1.2, but this is not the case in your examples. The "full-expression rule" (12.3 [class.temporary]/2) is based on the expression in which the temporary is *created*, but this is not the same expression as the lambda-expression itself. The latter just wraps the function call expression. 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! ]
|
Next
|
Last
Pages: 1 2 Prev: Template expansion during function template matching? Next: Implicit conversions for value subtyping |