title: singleton template as base class in C++
body: <p>According to <a href="https://stackoverflow.com/questions/1008019/c-singleton-design-pattern/1008289#1008289">C++ Singleton design pattern</a>  I wrote a singleton template</p>
  
  <pre><code>template &lt;typename T&gt;
  class Singleton
  {
      public:
          static T&amp; getInstance()
          {
              static T instance;
              return instance;
          }
      protected:
          Singleton() {}
          ~Singleton() {}
      public:
          Singleton(Singleton const &amp;) = delete;
          Singleton&amp; operator=(Singleton const &amp;) = delete;
  };
  </code></pre>
  
  <p>And then I wrote a Logger singleton(The first one)</p>
  
  <pre><code>class Logger                            // not derived Singleton&lt;Logger&gt;
  {
      friend class Singleton&lt;Logger&gt;;
      private:
          Logger(){};
          ~Logger(){};                          
      public:
          void print(void){std::cout &lt;&lt; "hello" &lt;&lt; std::endl;}
  };
  
  int main(void)
  {
      Singleton&lt;Logger&gt;::getInstance().print(); // use Singleton&lt;Logger&gt;
      return 0;
  }
  </code></pre>
  
  <p>but I can also(The second one)</p>
  
  <pre><code>class Logger:public Singleton&lt;Logger&gt;    // derived Singleton&lt;Logger&gt;
  {
      friend class Singleton&lt;Logger&gt;;
      private:
          Logger(){};
          ~Logger(){};                          
      public:
          void print(void){std::cout &lt;&lt; "hello" &lt;&lt; std::endl;}
  };
  
  int main(void)
  {
      Logger::getInstance().print();      // use Logger
      return 0;
  }
  </code></pre>
  
  <p>Then my questions are</p>
  
  <ol>
  <li>Is my singleton template correct?</li>
  <li>If singleton template is correct, the both Logger is correct?</li>
  <li>The difference between the first one and the second one,which one is better?</li>
  </ol>
  
  <p><strong>UPDATE</strong><br>
  After the answers,I learned that the first one allows to have several logger and the <code>singleton</code> class seems unneeded.SO I <em>discard</em> the first one.<br>
  About the seconde one I <em>omit</em> the destructor and then complier would generate a default inline public one. That seems more concise and more efficient.</p>
  
  <pre><code>class Logger:public Singleton&lt;Logger&gt;    // derived Singleton&lt;Logger&gt;
  {
      friend class Singleton&lt;Logger&gt;;
      private:
          Logger(){};                       
      public:
          void print(void){std::cout &lt;&lt; "hello" &lt;&lt; std::endl;}
  };
  </code></pre>