Can `auto &&` parameters be perfect forwarded?

1k views Asked by At

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)

1

There are 1 answers

1
Piotr Skotnicki On BEST ANSWER

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

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

DEMO