为什么以下代码有效:
template<typename T1>
void foo(T1 &&arg) { bar(std::forward<T1>(arg)); }
std::string str = "Hello World";
foo(str); // Valid even though str is an lvalue
foo(std::string("Hello World")); // Valid because literal is rvalue
但不是:
void foo(std::string &&arg) { bar(std::forward<std::string>(arg)); }
std::string str = "Hello World";
foo(str); // Invalid, str is not convertible to an rvalue
foo(std::string("Hello World")); // Valid
为什么示例2中的左值没有以与示例1中相同的方式解析?
另外,为什么标准认为需要在std :: forward与简单推导中提供参数类型很重要?无论类型如何,简单地呼唤前方都表现出意图。
如果这不是标准的东西而只是我的编译器,我使用的是msvc10,它可以解释蹩脚的C ++ 11支持。
由于
编辑1:将文字“Hello World”更改为std :: string(“Hello World”)以生成rvalue。
答案 0 :(得分:15)
首先,read this可以全面了解转发。 (是的,我在其他地方委托了大部分答案。)
总而言之,转发意味着左值保持左值并且左值保持左值。你不能用一种类型做到这一点,所以你需要两个。因此,对于每个转发的参数,您需要该参数的两个版本,该函数需要2个 N 组合。您可以编码该函数的所有组合,但如果您使用模板,则会根据需要为您生成各种组合。
如果您正在尝试优化副本和移动,例如:
struct foo
{
foo(const T& pX, const U& pY, const V& pZ) :
x(pX),
y(pY),
z(pZ)
{}
foo(T&& pX, const U& pY, const V& pZ) :
x(std::move(pX)),
y(pY),
z(pZ)
{}
// etc.? :(
T x;
U y;
V z;
};
然后你应该停下来这样做:
struct foo
{
// these are either copy-constructed or move-constructed,
// but after that they're all yours to move to wherever
// (that is, either: copy->move, or move->move)
foo(T pX, U pY, V pZ) :
x(std::move(pX)),
y(std::move(pY)),
z(std::move(pZ))
{}
T x;
U y;
V z;
};
您只需要一个构造函数。 指南:如果您需要自己的数据副本,请在参数列表中创建该副本;这使得决定复制或移动到调用者和编译器。