Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can `auto &&` parameters be perfect forwarded?

For template functions I use perfect forwarding like this:

template<typename T>
void f (T && v)
{
  g (std::forward<T> (v));
}

How do I perfect forward auto && parameters in C++14 lambda expressions?

auto f = [] (auto && v)
  {
    g (std::forward<??> (v));
  };

(Tried to google for it but didn't get any good hits on the keywords I picked)

like image 393
Just another metaprogrammer Avatar asked Jun 10 '15 21:06

Just another metaprogrammer


1 Answers

Yes, they can be perfect-forwarded by means of decltype() specifier:

auto f = [](auto&& v)
{
    g(std::forward<decltype(v)>(v));
    //             ~~~~~~~~~~^
};

DEMO

like image 56
Piotr Skotnicki Avatar answered Oct 17 '22 03:10

Piotr Skotnicki