问题描述
在 VS2010 中 std::forward 定义如下:
In VS2010 std::forward is defined as such:
identity
似乎仅用于禁用模板参数推导.在这种情况下故意禁用它有什么意义?
identity
appears to be used solely to disable template argument deduction. What's the point of purposefully disabling it in this case?
推荐答案
如果将 X
类型的对象的右值引用传递给采用 T&& 类型的模板函数
作为其参数,模板参数推导推导出 T
为 X
.因此,参数的类型为 X&&
.如果函数参数是左值或 const 左值,则编译器将其类型推导为该类型的左值引用或 const 左值引用.
If you pass an rvalue reference to an object of type X
to a template function that takes type T&&
as its parameter, template argument deduction deduces T
to be X
. Therefore, the parameter has type X&&
. If the function argument is an lvalue or const lvalue, the compiler deduces its type to be an lvalue reference or const lvalue reference of that type.
如果 std::forward
使用模板参数推导:
If std::forward
used template argument deduction:
由于带有名称的对象是左值
,因此std::forward
唯一一次正确地转换为T&&
参数是一个未命名的右值(如 7
或 func()
).在完美转发的情况下,您传递给 std::forward
的 arg
是一个左值,因为它有一个名称.std::forward
的类型将被推导出为左值引用或常量左值引用.引用折叠规则将导致 std::forward 中 static_cast
T&&
始终解析为左值引用或常量左值引用.
Since objects with names are lvalues
the only time std::forward
would correctly cast to T&&
would be when the input argument was an unnamed rvalue (like 7
or func()
). In the case of perfect forwarding the arg
you pass to std::forward
is an lvalue because it has a name. std::forward
's type would be deduced as an lvalue reference or const lvalue reference. Reference collapsing rules would cause the T&&
in static_cast<T&&>(arg)
in std::forward to always resolve as an lvalue reference or const lvalue reference.
示例:
这篇关于为什么使用 std::forward 禁用模板参数推导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!