Given the following code snippet:
struct T {}; std::function<T&(T&)> f = [](T& obj) -> T& { return obj; };
I was wondering if it is possible to infer the correct lambda return type (i.e. T&
) without using trailing return type syntax.
Obviously, if I remove -> T&
then a compile-time error will occur in that the deduced type would be T
.
In C++14, you can use [](T& obj) -> decltype(auto) { return obj; }
. In that case, the return type of f
is deduced from the declared type of obj
(i. e. T&
in this case).
No, but in C++14 you could use auto&
as the trailing-return-type. If it's typing you're worried about, and compiler upgrades don't worry you at all, then this mostly solves your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With