I know the following is a thread-safe way to implement a singleton in C++11:
Foo* getInst()
{
static Foo* inst = new Foo(...);
return inst;
}
I read in this answer that the following is also thread-safe:
Foo& getInst()
{
static Foo inst(...);
return inst;
}
Is it really thread-safe?
Isn't it a problem that the instance of Foo will be allocated in a single stack frame will not be allocated on the heap?
If it is thread-safe, is there a strong reason to prefer one to the other?