title: Is it better to declare the static singleton object outside of the static instance getter method?
body: <p>While reading the most upvoted <a href="https://stackoverflow.com/a/1008289/10400598">question</a> explaining the Singleton Pattern in C++, I realized some performance issues. The answer proposes the below form for implementing the pattern.</p>
  <pre><code>class SomeClass {
  public: /** Singleton **/
      static SomeClass&amp; instance() {
          static SomeClass singleInstance;
  
          return singleInstance;
      };
  
  private: 
      SomeClass() = default;
      SomeClass(const SomeClass&amp;) = delete;
  };
  </code></pre>
  <p>When I implement the Singleton in the suggested form, the assembly of the program is as follows. Looking at the output below, I can easily say that the <code>instance()</code> method includes a branch to test whether it is the first call to the function or not <em>(e.g. whether the static instance was created or not)</em>.</p>
  <pre><code>SomeClass::instance():
          push    rbp
          mov     rbp, rsp
          movzx   eax, BYTE PTR guard variable for SomeClass::instance()::singleInstance[rip]
          test    al, al
          sete    al
          test    al, al
          je      .L2
          mov     edi, OFFSET FLAT:guard variable for SomeClass::instance()::singleInstance
          call    __cxa_guard_acquire
          test    eax, eax
          setne   al
          test    al, al
          je      .L2
          mov     edi, OFFSET FLAT:guard variable for SomeClass::instance()::singleInstance
          call    __cxa_guard_release
  .L2:
          mov     eax, OFFSET FLAT:SomeClass::instance()::singleInstance
          pop     rbp
          ret
  </code></pre>
  <p>Then, I tried to separate the declaration of the static object from the static <code>instance()</code> method like below.</p>
  <pre><code>class SomeClass {
  public: /** Singleton **/
      static SomeClass&amp; instance() {
          return singleInstance;
      };
  
  private: 
      SomeClass() = default;
      SomeClass(const SomeClass&amp;) = delete;
      
      static SomeClass singleInstance;
  };
  
  SomeClass SomeClass::singleInstance; // In the source file
  </code></pre>
  <p>The output of the second program is, as expected, different from the first one. It doesn't include a branch that tests the existence of the Singleton object.</p>
  <pre><code>SomeClass::instance():
          push    rbp
          mov     rbp, rsp
          mov     eax, OFFSET FLAT:SomeClass::singleInstance
          pop     rbp
          ret
  </code></pre>
  <p>I've found a similar <a href="https://stackoverflow.com/questions/34396373/singleton-instance-as-static-field-vs-static-variable-in-getinstance-method">question</a> explaining the reason for using the in-class static definition of the Singleton instance.</p>
  <p>Is it really better to declare the Singleton object outside of the static getter function if the static initialization order isn't a problem?</p>