重载的参数std::forward
(使用std::remove_reference
)删除任何本来会发生的引用折叠和参数推导,并强制左值和右值引用绑定到正确的重载。这也是在不添加或删除任何可能const
是原始参数的一部分(或不是一部分)的情况下完成的T
(即我们不使用 aconst T&
因为它本质上将 a 添加const
到T&&
没有的右值,但它们都可以绑定到相同的右值)。
在右值引用重载中进行关键错误检查;这是一个健全性检查,以确保std::forward
在提供右值时不会调用它来转发到左值引用;基本上确保代码std::forward<int&>(42);
无法编译。std::forward
旨在用于公式std::forward<T>
where T
is from the deduced contextT&&
如参考中所述。重载影响与带有所需错误检查的条件返回相同的结果。
的实现std::forward
面向 Scott Meyers 在他的Going Native 2013演讲中谈到的“条件转换”。Scott Meyers 在他的演讲中给出了以下伪代码来解释std::forward
(大约 20 分钟)的工作原理;
template <typename T>
T&& forward(T&& param) { // T&& here is formulated to disallow type deduction
if (is_lvalue_reference<T>::value) {
return param; // return type T&& collapses to T& in this case
}
else {
return move(param); // return type is T&&
}
}
std::forward
如果其参数的类型是左值,则实现为返回左值引用,如果std::move
其参数类型的类型是右值,则返回右值引用(等效于 )。
TL;DR 为什么需要它?基本上; 它可以防止错误使用std::forward
(例如悬空引用、对不再可用的临时对象的引用、修改/修改的文字等)。