Consider the following program:
class C { ... }; const C f() { C ret; cout << &ret << endl; return ret; } int main() { C value = f(); cout << &value << endl; } result: // note the address are the same 0x7ffdd24b26e0 0x7ffdd24b26e0 The variable 'ret' in function f() and variable 'value' has the same memory address so it seems 'value' is not a copy of 'ret'. The variable 'ret' is a stack variable so it should be invalidated after f() returns. So why c++ allow returning a stack value inside a function?
g++ version:
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4