#include <iostream>
int foo()
{
return 0;
}
int main()
{
const int& a = foo();
std::cout << &a << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在此代码中,a绑定到右值.取其地址是否合法?(并且通过合法我的意思是:在代码格式不正确?我是否导致了未定义的行为?)
这可以.在C++ 11中,您甚至可以这样做:
int&& a = foo();
a = 123;
Run Code Online (Sandbox Code Playgroud)
你可以考虑像这样的临时工(概念上和一般情况下):
x = func(); // translated as:
auto __temporary = func();
x = __temporary;
__destruct_value_now_and_not_later(__temporary);
Run Code Online (Sandbox Code Playgroud)
除非x是引用类型的定义,否则编译器会注意到您有意引用临时值并通过删除早期破坏代码来延长其生命周期,使其成为常规变量.
| 归档时间: |
|
| 查看次数: |
522 次 |
| 最近记录: |