C++ 0x编译器优化是否合法
int func(int&& a){
a = 3;
return a;
}
Run Code Online (Sandbox Code Playgroud)
至
int func(int&& a){
return 3;
}
Run Code Online (Sandbox Code Playgroud)
?(或另一个POD)
不是那样的本身,因为函数必须修改变量a是等价的.也就是说,在内联和一些微不足道的优化后,结果将是相同的:
int x = func(5);
// pseudo-inlined:
int __temp = 5; // the temporary created by binding 5 to int&&
__temp = 3; // a = 3;
int x = __temp; // return a;
// constant propagation:
int __temp = 5;
__temp = 3;
int x = 3;
// dead-code elimination:
int x = 3;
Run Code Online (Sandbox Code Playgroud)
请注意,结果与使用func和内联的第二个定义相同,只是因为未使用临时变量.这表明这两个函数一般不相同:
int modifyme = 100;
int x = func(std::move(modifyme));
// modifyme must be 3, second function definition wouldn't do that
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
300 次 |
| 最近记录: |