title: Construct on first use + forced initialization to solve static initialization order fiasco?
body: <p>It is known that <a href="http://www.parashift.com/c++-faq/static-init-order.html" rel="nofollow">static initialization order fiasco (SIOF)</a> can be solved by using <a href="https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Construct_On_First_Use" rel="nofollow">construct on first use idiom (COFU)</a>. However, this solution may introduce <a href="http://blogs.msdn.com/b/oldnewthing/archive/2004/03/08/85901.aspx" rel="nofollow">the issue with race conditions</a> when the variable is being initialized from multiple threads. </p>
  
  <p>I tried to avoid both SIOF and race conditions in my project in a following way. I used COFU. And I forced initialization of target variable by using non-local variable (<code>c_unusedNonLocal</code>) in cpp. Thus, thanks to <code>c_unusedNonLocal</code>, even if there are no non-locals which access target variable via <code>B::getA</code> it is initialized anyway. What is important, it is initialized before <code>main</code> in a single thread. So, with read-only access there should be no race conditions and no SIOF.</p>
  
  <p>Is this a feasible solution? Are there any possible pitfalls or I mistaken about initialization in a single thread or whatever?</p>
  
  <p>Thanks.</p>
  
  <p>a.h</p>
  
  <pre><code>struct A
  {
      A() {}
  };
  </code></pre>
  
  <p>b.h</p>
  
  <pre><code>struct B
  {
      static const A&amp; getA();
  };
  </code></pre>
  
  <p>b.cpp</p>
  
  <pre><code>// force initialization before 'main'
  // assuming that this happens in a single thread (thus no race conditions)
  static const A&amp; c_unusedNonLocal = B::getA();
  
  const A&amp; B::getA()
  {
      static const A* result = new A();
      return *result;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>int main() {
      // _read only_ access via B::getA is now thread safe
      // because object is guaranteed (?) to be initialized before main
      return 0;
  }
  </code></pre>