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)
是否可以将C++(或C++ 0x)中的局部变量的所有权转移给函数,在返回后将其保留为未定义,因此可以进行优化?
struct A {
int a[100000];
};
int func(A& s){
//s should now be "owned" by func and be undefined in the calling function
s.a[2] += 4;
return s.a[2];
}
int main(){
A s;
printf("%d\n", func(s));
//s is now undefined
}
Run Code Online (Sandbox Code Playgroud)
我希望函数"func"被优化为简单地返回sa [2] +4,但不改变内存中的实际值,就像"s"是"func"中的局部变量一样.如果无法在标准C++中完成,是否可以在g ++中进行一些扩展?