What is the return type of a lambda expression if an item of a vector is returned?

后端 未结 3 1516
无人及你
无人及你 2020-12-10 10:24

Consider the following snippet:

#include 
#include 
#include 

int main() 
{
    std::vectorv = {         


        
3条回答
  •  旧巷少年郎
    2020-12-10 10:59

    The return type of a lambda uses the auto return type deduction rules, which strips the referenceness. (Originally it used a slightly different set of rules based on lvalue-to-rvalue conversion (which also removed the reference), but that was changed by a DR.)

    Hence, [&v](int i) { return v[i];}; returns int. As a result, in std::function f = [&v](int i) { return v[i];};, calling f() returns a dangling reference. Binding a reference to a temporary extends the lifetime of the temporary, but in this case the binding happened deep inside std::function's machinery, so by the time f() returns, the temporary is gone already.

    g(3) is fine because the const int & returned is bound directly to the vector element v[i], so the reference is never dangling.

提交回复
热议问题