临时绑定到一个左值引用 [英] Binding temporary to a lvalue reference

查看:80
本文介绍了临时绑定到一个左值引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

  string three()
{
returnthree
}

void mutate(string& ref)
{
}

int main()
{
mutate(three());
return 0;
}



您可以看到我正在传递 three到 方法。这段代码编译得很好。我的理解是,临时不能分配给非const引用。



编辑:



编译器尝试:VS 2008和VS2010测试版

解决方案

用于编译VC6编译器,所以我想保持向下兼容性VS2008是支持这种非标准的扩展。尝试使用 / Za (停用语言扩展)标志,您应该获得一个错误。


I have the following code

string three()
{
    return "three";
}

void mutate(string& ref)
{
}

int main()
{
    mutate(three()); 
    return 0;
}

You can see I am passing three() to mutate method. This code compiles well. My understanding is, temporaries can't be assigned to non-const references. If yes, how this program is compiling?

Any thoughts?

Edit:

Compilers tried : VS 2008 and VS2010 Beta

解决方案

It used to compile in VC6 compiler, so I guess to maintain backward comptibility VS2008 is supporting this non-standard extension. Try with /Za (disable language extension) flag, you should get an error then.

这篇关于临时绑定到一个左值引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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