Although the question specifically asks the question about C ++ 11, for others who stumble over it and have access to the C ++ 14 compiler, C ++ 14 now allows you to infer return types for regular functions. Thus, the example in the question can be adjusted just to work as desired, simply by dropping the sentence -> decltype ... after the list of function parameters:
auto retFun() { return [](int x) { return x; } }
Note, however, that this will not work if more than one return <lambda>; appears in the function return <lambda>; . This is due to the fact that the restriction on the return of the return type is that all return statements must return expressions of the same type, but each lambda object gets its own unique type by the compiler, therefore the expressions return <lambda>; will have different types.
Anthony Hall Jun 17 '16 at 23:40 2016-06-17 23:40
source share