Is it ALWAYS safe to return temporary variables ?
Is it always safe to return temporary stack variables from a function as long as you don't return a reference or a pointer ? So is this always gauranteed to be safe, on all platforms/compilers/targets ? The compiler is never going to generate ASM that uses the location of testA in an unsafe manner ? (e.g. a location on the stack that is trashed when getA() returns)
Code:
MyBigClass getA()
{
.
.
MyBigClass testA;
testA.DoStuff(x,y,z);
return testA;
}
int main(int argc, char* argv[])
{
.
.
MyBigClass a0=getA();
.
.
a0.Dump();
}
I have always assumed it was (that it would always return a copy of testA no matter what) but someone just pointed this out at a potential bug. I'm not sure whether to beleive him :-)