Binding temporary to a lvalue reference(将临时绑定到左值引用)
问题描述
我有以下代码
string three()
{
return "three";
}
void mutate(string& ref)
{
}
int main()
{
mutate(three());
return 0;
}
你可以看到我正在将 three() 传递给 mutate 方法.这段代码编译得很好.我的理解是,不能将临时对象分配给非常量引用.如果是,这个程序是如何编译的?
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?
有什么想法吗?
尝试过的编译器:VS 2008 和 VS2010 Beta
Compilers tried : VS 2008 and VS2010 Beta
推荐答案
以前是用VC6编译器编译的,所以我猜为了保持向后兼容VS2008是支持这个非标准扩展的.尝试使用 /Za (禁用语言扩展)标志,那么你应该得到一个错误.
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.
这篇关于将临时绑定到左值引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将临时绑定到左值引用
基础教程推荐
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- c++ STL设置差异 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
