title: c++ template weird optimization
body: <p>I wrote a singleton template class just like boost does:</p>
  
  <pre><code>template &lt;typename _T&gt;
  class Singleton
  { 
      public :
      static _T* Instance()
      {
          static _T obj;
          return &amp;obj;
      }
  
  protected :
      Singleton() {}
  
  private :
      struct ObjectCreator
      {
          ObjectCreator()
          {
              Singleton&lt;_T&gt;::instance();
          }
      };
  
      static ObjectCreator object_creator;
  };
  
  template &lt;typename _T&gt;
  typename Singleton&lt;_T&gt;::ObjectCreator Singleton&lt;_T&gt;::object_creator;
  </code></pre>
  
  <p>And I wrote the main function to test it.</p>
  
  <pre><code>#include "Singleton.h"
  class A : public Singleton &lt;A&gt;
  {
      public:
          int a;
  };
  
  
  int main()
  {
      A::Instance()-&gt;a = 2;
  }
  </code></pre>
  
  <p>I know I mistyped Instance in <code>ObjectCreator</code>'s constructor, the weird thing is I can compile it correctly by gcc-4.4.7, then I used clang-6.0, it hit me with the typo.</p>
  
  <p>I guess gcc can do some optimization, because I did not do anything with <code>ObjectCreator</code>, so it ignored the error code.</p>
  
  <p>I have two questions:</p>
  
  <ol>
  <li>What should I do to make gcc report me that error (<strong>without changing my code</strong>), like add some compiler flag?  </li>
  <li>If anyone has a more reliable explanation for this? Some official doc would do.  </li>
  </ol>
  
  <p><strong>Ps: I am aware that boost would add a <code>do_nothing</code> function in <code>ObjectCreate</code> and call it from <code>Singleton&lt;_T&gt;:: Instance()</code> to avoid this optimization.</strong></p>