ProgrammingHardProduct-company placement · C++

In C++11 and later, which statement about this 'Meyers singleton' is correct?

In C++11 and later, which statement about this 'Meyers singleton' is correct?
Logger& instance() {
    static Logger log;
    return log;
}
  1. A. Initialisation of log is thread-safe: if several threads call instance() at once, exactly one constructs it
  2. B. It is not thread-safe unless a mutex is added
  3. C. log is constructed before main starts
  4. D. A new Logger is created on every call

Correct answer: A. Initialisation of log is thread-safe: if several threads call instance() at once, exactly one constructs it

Since C++11 the language guarantees that a block-scope static is initialised exactly once even under concurrent first calls (other threads wait). It is constructed on first use, not before main.

More OOP with C++ and Java questions

Exams that ask this topic