In order to understand copy elision I wrote a small example
#include "iostream"
using namespace std;
struct Foo
{
Foo() { cout << "constructor was called" << endl; }
~Foo() { cout << "destructor was called" << endl; }
};
Foo f()
{
Foo foo;
return foo;
}
int main()
{
Foo bla = f();
return 0;
}
output :
constructor was called
destructor was called
destructor was called
As I understand it, in the f() function the foo object is constructed directly in the memory space where bla is allocated so the constructor is called only once. I understand that when we exit the main() function, the bla object goes out of scope and the destructor is called. But why is the destructor called twice? Is the destructor of foo called when we exit the scope of the f() function even though the object is not destroyed and used by the caller?