By default, an auto-detected lambda type is a minor version of a type
... the return type is the type of the returned expression (after lvalue-to-rvalue, from array to pointer or implicit conversion of function-to-pointer); (a source)
If you need a return type with a link, you will need to specify it more explicitly. Here are a few options:
[&]() -> decltype( makeRefA() ) { return makeRefA()); };
or just completely explicitly about the return type with ->
[&]() -> const A& { return makeRefA(); }
If you are using C ++ 14 just use decltype(auto) ,
[&]() -> decltype(auto) { return makeRefA(); }
Rules for decltype can be tricky at times. But the fact that makeRefA() is an expression (as opposed to simply naming a variable) means that the type of the expression ( const A& ) returns decltype( makeRefA() ) correctly.
Aaron mcdaid
source share