上篇博客对右值、右值引用都做了简要介绍。
我们也要时刻清醒,有时候右值会转为左值,左值会转为右值。
(也许“转换”二字用的不是很准确)
如果我们要避免这种转换呢?
我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完美转发。
std::forward就可以保存参数的左值或右值特性。
因为是这样描述的:
When used according to the following recipe in a function template, forwards the argument to another function with the value category it had when passed to the calling function.
例子:
template<class T>
void wrapper(T&& arg)
{
foo(std::forward<T>(arg)); // Forward a single a