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 类型的临时值初始化无效::字符串
- VS2008 还不错,至少它给出了一个编译警告:
警告 C4239:使用了非标准扩展:“正在初始化”:
从
std::string
到std::string &
的转换 引用只能绑定(bind)到左值 - 问题来了 - VS2010(SP1) 编译正常,没有任何
错误或警告,为什么??!!
我知道 VS2010 中的 rvalue reference 可用于与 rvalue 绑定(bind),但我没有使用
&&
,而是在演示代码中,我只是使用非常量左值引用!
有人能帮我解释一下 VS2010 的行为吗?这是一个错误!? 谢谢
最佳答案
这是 VS 编译器的一个已知问题/功能。他们一直允许这样做,并且似乎没有任何插入删除该扩展名。
关于c++ - 一个 VS2010 错误?允许在没有警告的情况下绑定(bind)对右值的非常量引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146071/