【发布时间】:2011-08-25 11:16:21
【问题描述】:
string foo() { return "hello"; }
int main()
{
//below should be illegal for binding a non-const (lvalue) reference to a rvalue
string& tem = foo();
//below should be the correct one as only const reference can be bind to rvalue(most important const)
const string& constTem = foo();
}
- GCC 是给出编译错误的好方法:从
std::string类型的临时变量中对std::string&类型的非常量引用进行无效初始化 - VS2008 还不错,至少它给出了一个编译警告:
警告 C4239:使用了非标准扩展:“正在初始化”:
从
std::string到std::string &的转换一个非常量 引用只能绑定到左值 - 问题来了 - VS2010(SP1) 没有任何问题
错误或警告,为什么??!!
我知道 VS2010 中的 右值引用 可以用来绑定右值,但我没有使用
&&,而是在演示代码中,我只是使用非 const 左值引用!
有人能帮我解释一下这里 VS2010 的行为吗?它是一个错误!? 谢谢
【问题讨论】:
-
您的警告设置是什么?也许是您的配置发生了变化,而不是它们的实现?
-
我一直在使用VS2008,但最近安装了VS2010,所以VS10应该使用它的默认设置......
-
那恐怕警告门槛根本不够高。使用
\W4激活4xxx范围内的警告。 -
@Gob00st - 所以抱怨默认设置不是你需要的?
-
@Bo Persson - 我对 VS2010 的默认设置不满意,它没有给出任何错误或警告! VS2008 确实给出了一个警告,恕我直言总比没有好。
标签: c++ visual-studio-2010 reference lvalue rvalue