C++0x编译器优化是否合法
int func(int&& a){
a = 3;
return a;
}
到
int func(int&& a){
return 3;
}
? (或另一个 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;
请注意,结果与使用 func
的第二个定义和内联结果相同,只是因为未使用临时变量。这表明这两个函数通常不等价:
int modifyme = 100;
int x = func(std::move(modifyme));
// modifyme must be 3, second function definition wouldn't do that
关于c++ - 这种 C++0x 优化合法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7153710/