Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfect forwarding in a lambda?

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?

like image 348
Vincent Avatar asked Mar 15 '17 00:03

Vincent


1 Answers

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));
} //                      ^^^^^^^^^^^
like image 138
Kerrek SB Avatar answered Oct 13 '22 22:10

Kerrek SB