I have something like this in my CPP file to initialize an object outside any class. I've simplified it but the point is there is some complex initialization going on but I want a single instance created:
static MyBigObject o = []()
{
MyBigObject ret;
ret.Init();
return ret;
}();
My question is, does this object get copied from ret
to o
or created in-place due to compiler cleverness? There might be a proper term for "created in place" - emplaced?
I am working in C++17, if it's pertinent (i.e. the answer depends on language version). Is there a definite answer, or could this be a compiler-specific optimisation?
In this case: this depends on your compiler. This is named return value optimization, which is optionally elided. C++17 allows, but not requires, the copy to be elided even if the object has observable side effects in its copy/move constructors (and its destructor).