Perfect forwarding is possible when the template parameter type contains a category of values. (If this sentence does not make sense, take a moment to familiarize yourself with this problem .)
Given:
template <typename T> void foo(T&& x);
In the body foo , T will either take the form U or U& . The first means that we were given an rvalue, the latter means that we passed an lvalue. We can forward this fact as follows:
template <typename T> void foo(T&& x) { bar(std::forward<T>(x)); }
Pass the lvalue value to foo , bar gets the same lvalue value. Pass the value of r foo , bar gets the value of r.
If you cannot distinguish between a category of values, a transfer is not necessary. This is only useful when you have a template parameter that is displayed in the same way as described above. So yes, here it is useless:
template <typename T> void foo(const T& x) {
GManNickG
source share