With a function, one can write:
template <class T> void f(T&& x) {myfunction(std::forward<T>(x));}
but with a lambda, we don't have T
:
auto f = [](auto&& x){myfunction(std::forward</*?*/>(x));}
How to do perfect-forwarding in a lambda? Does decltype(x)
work as the type in std::forward
?
The canonical way to forward a lambda argument that was bound to a forwarding reference is indeed with decltype
:
auto f = [](auto&& x){
myfunction(std::forward<decltype(x)>(x));
} // ^^^^^^^^^^^
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