为什么我应该显式地将typename传递给std::forward
Why should I explicitly pass typename to std::forward?
为什么需要在std::forward中显式指示模板参数的类型?
template <class T> void foo (T&& x) {
goo (x); // always an lvalue
goo (std::forward<T>(x)); // rvalue if argument is rvalue
}
考虑std::正向实现:
template <typename T>
T&& forward(std::remove_reference_t<T>& x)
{
return static_cast<T&&>(x);
}
和std::remove_reference实现:
template< class T > struct remove_reference {typedef T type;};
template< class T > struct remove_reference<T&> {typedef T type;};
template< class T >
using remove_reference_t = typename remove_reference<T>::type;
std::forward()
中的参数类型为:
remove_reference<T>::type
这里T
位置是作用域分辨率运算符::
的左边;非推导上下文";(请参阅cpprreference上的非推导上下文(。因为它不是自动推导出来的,所以您必须自己提供类型。
相关文章:
- 完美前进使用 std::forward vs RefRefCast
- 你如何理解"std: :forward is just syntactic sugar"?这是真的吗?
- "std::forward"和"std::move"真的不生成代码吗?
- 在C++中使用 std::forward 的多个参数
- 如何使用 std::forward 精确地评估参数包的扩展?
- 如何通过通用引用或std::forward将这三个c++模板函数合并为一个
- 自 C++11 年以来对 std::forward 实施的理解
- 当将参数包传递给另一个可变参数模板函数时,我是否必须使用 std::forward
- 为什么要在概念中使用std::forward
- 寻求对std::forward的澄清
- 为什么 std::forward 将左值和右值转换为右值引用?
- std::forward 如何推断出"_Ty"的类型?
- std::function operator() 和 std::forward 中发生了什么?
- How to std::forward( *this )
- 在哪些情况下,从 std::forward 分配比从 std::move 分配更可取?为什么
- 如何在varadic/template类构造函数中正确使用std::forward
- 包含 std::forward 会产生错误,但其省略会编译.为什么
- Returning std::make_pair with std::forward
- C 11构造函数参数:std ::移动和值或std :: forward and rvalue参考
- std::forward and operator()