I know this is an often asked question, but as there are so many variants, I'd like to re-state it, and hopefully have an answer reflecting the current state. Something like
Logger& g_logger() {
static Logger lg;
return lg;
}
Is the constructor of variable lg guaranteed to run only once?
I know from previous answers that in C++03, this is not; in C++0x draft, this is enforced. But I'd like a clearer answer to
The relevant section 6.7:
such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. [...] If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.
Then there's a footnote:
The implementation must not introduce any deadlock around execution of the initializer.
So yes, you're safe.
(This says nothing of course about the subsequent access to the variable through the reference.)