c++ - 一个 VS2010 错误?允许在没有警告的情况下绑定(bind)对右值的非常量引用?

标签 c++ visual-studio-2010 reference lvalue rvalue

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();   
}
  1. GCC 是给出编译错误的好工具:std::string& 类型的非常量引用从 std 类型的临时值初始化无效::字符串
  2. VS2008 还不错,至少它给出了一个编译警告: 警告 C4239:使用了非标准扩展:“正在初始化”: 从 std::stringstd::string & 的转换 引用只能绑定(bind)到左值
  3. 问题来了 - VS2010(SP1) 编译正常,没有任何 错误或警告,为什么??!! 我知道 VS2010 中的 rvalue reference 可用于与 rvalue 绑定(bind),但我没有使用 &&,而是在演示代码中,我只是使用非常量左值引用!

有人能帮我解释一下 VS2010 的行为吗?这是一个错误!? 谢谢

最佳答案

这是 VS 编译器的一个已知问题/功能。他们一直允许这样做,并且似乎没有任何插入删除该扩展名

关于c++ - 一个 VS2010 错误?允许在没有警告的情况下绑定(bind)对右值的非常量引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146071/

相关文章:

visual-studio-2010 - 如何使用 Gallio 在 Visual Studio 的测试系统中运行 MSpec?

c++ - 如何在单个声明中设置继承的 C++ 类/结构变量

c++ - 英特尔 PIN :Initial Set up of Intel PIN tool

c++ - 不完整类型 boost function_traits 的无效使用

c++ - Qt C++如何输入值

c++ - Visual Studio "Application failed to start because the application configuration is incorrect"错误

c++ - 将 vector 引用复制到另一个

r - 有关 S4 对象、方法和 R 编程的来源

Java 引用 ArrayList

c++ - std::ostream 到 QDataStream