c++ - Return local variable to const ref from lambda -


const tbigtype& = [](){     tbigtype result;     // ...     return result; }();  use(a); // const ref 

is ok capture result in const ref this?

your lambda returns prvalue, call expression temporary object, lifetime extended because bound reference.

the situation entirely analogous following, simpler example:

int f() { return 12; }  const int& = f(); 

here a bound temporary object of type int , value 12.

if not specify return type of lambda, return type either void or object type. if want lambda return lvalue or xvalue, explicitly need specify return type, e.g. -> int&, -> auto&, -> decltype(auto), etc.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -