一个VS2010错误?允许绑定非const引用到右值WITHOUT EVEN警告? [英] One VS2010 bug ? Allowing binding non-const reference to rvalue WITHOUT EVEN a warning?

查看:116
本文介绍了一个VS2010错误?允许绑定非const引用到右值WITHOUT EVEN警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 :: 的非临时引用无效初始化 std :: string& 字符串

  2. VS2008并不是太糟糕,至少它会产生编译警告
    警告C4239: :'initializing':
    std :: string 转换为 std :: string& -const
    引用只能绑定到一个左值

  3. 这里有问题 - VS2010(SP1)comples罚款没有任何
    错误或警告,WHY? !
    我知道VS2010中的右值引用可以用于与右值绑定,但我不使用&& 示例代码,我只是使用非常见的值引用!

  1. GCC is the good one to give a compile error: invalid initialization of non-const reference of type std::string& from a temporary of type std::string
  2. VS2008 is not too bad as at least it gives a compile warning: warning C4239: nonstandard extension used : 'initializing' : conversion from std::string to std::string & A non-const reference may only be bound to an lvalue
  3. Here comes the problematic one - VS2010(SP1) comples fine WITHOUT any error or warning, WHY ??!! I know rvalue reference in VS2010 can be used to bind with rvalue but I am NOT using &&, instead in the demo code, I was just using non-const lvalue reference !

可以somone帮我解释VS2010的行为吗?这是一个bug!
感谢

Can somone help me explain the behavior of VS2010 here? Is it a bug !? Thanks

推荐答案

这是VS编译器的一个已知问题/功能。他们总是允许这样做,似乎没有任何推动删除扩展

That is a known issue/feature of the VS compilers. They have always allowed that and there does not seem to be any push into removing that extension.

这篇关于一个VS2010错误?允许绑定非const引用到右值WITHOUT EVEN警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆