【发布时间】:2013-05-25 15:13:49
【问题描述】:
当我遇到这个案例时,我正在追踪一个编译错误:
struct Y
{
int&& y;
Y(int&& y)
: y(y)
{
}
};
struct ZZ {};
struct Z
{
ZZ&& z;
Z(ZZ&& z)
: z(z)
{
}
};
这些都失败了:
exec.cpp: In constructor ‘Y::Y(int&&)’:
exec.cpp:57:10: error: invalid initialization of reference of type ‘int&&’ from expression of type ‘int’
exec.cpp: In constructor ‘Z::Z(ZZ&&)’:
exec.cpp:67:10: error: invalid initialization of reference of type ‘ZZ&&’ from expression of type ‘ZZ’
但我不确定为什么。这里有什么问题?
我正在使用带有 -std=gnu++0x 选项的 g++4.5.3,但它也与 -std=c++0x 选项一起使用。
【问题讨论】:
-
我很难找到将右值引用存储为成员的情况。您能否详细说明问题的背景?
-
实际上,我只是想了解一下移动语义以及 && 的真正含义。我不确定是否有原因。
-
更新:我认为将 r 值引用作为成员几乎没有任何价值,并且可能非常危险,如果具有该成员的对象具有任何重要的生命周期。可能有一些价值,但看起来很小。
标签: c++ g++ move-semantics