title: C++ Singletons: how good is this solution? Advantages/disadvantages, alternatives
body: <p>I am working on a C++ project having multiple classes that must be singletons, <strong>with dependencies between them</strong> (order of initialization matters).</p>
  
  <p>I have come up with this solution:</p>
  
  <ol>
  <li>All classes which I want to be singletons have <strong>protected constructors</strong>, e.g.:</li>
  </ol>
  
  <blockquote>
  <pre><code>class MySingleton1
  {
  protected:
      MySingleton1();
  }
  </code></pre>
  </blockquote>
  
  <ol start="2">
  <li>Have a source file <strong>singleton_factory.cpp</strong> containing an instantiated class <strong>Singletons</strong> which <strong>derives from all classes which I want to be singletons</strong>, like this:</li>
  </ol>
  
  <blockquote>
  <pre><code>#include "MySingleton1.hpp"
  #include "MySingleton2.hpp"
  
  class Singletons : public MySingleton1, public MySingleton2 {}
  static Singletons s_singletons;
  </code></pre>
  </blockquote>
  
  <ol start="3">
  <li>Still in <strong>singleton_factory.cpp</strong>, for <strong>every singleton type</strong>, also implement a specialization of a <strong>getSingleton</strong> function:</li>
  </ol>
  
  <blockquote>
  <pre><code>template&lt;&gt;
  MySingleton1&amp; getSingleton()
  {
      return s_singletons;
  }
  
  template&lt;&gt;
  MySingleton2&amp; getSingleton()
  {
      return s_singletons;
  }
  </code></pre>
  </blockquote>
  
  <ol start="4">
  <li>The specializations of <strong>getSingleton</strong> will be "hid" under the generic templated variant, in <strong>singleton_factory.hpp</strong>:</li>
  </ol>
  
  <blockquote>
  <pre><code>template &lt;class TSingleton&gt;
  TSingleton&amp; getSingleton();
  </code></pre>
  </blockquote>
  
  <p>Advantages:</p>
  
  <ul>
  <li><p><strong>Low-coupling</strong>:</p>
  
  <ul>
  <li><strong>Singleton classes don't need to be "aware" of the Singletons class</strong>, the only need to hide their constructor under a protected qualifier (and that is not even mandatory, only good practice). The only code actually aware of the Singletons class is <strong>singleton_factory.cpp</strong></li>
  <li>Skinny dependencies for <strong>concrete instance</strong>: code that wants to use a singleton of type T <strong>only needs to include the header of type T</strong> and the skinny <strong>singleton_factory.hpp</strong></li>
  </ul></li>
  <li><p><strong>Order of initialization</strong> can be controlled by <strong>changing the order of inheritance</strong> of the Singletons class</p></li>
  <li><strong>No lazy initialization</strong> => <strong>thread-safe</strong>?</li>
  <li><strong>getSingleton()</strong> is fast, <strong>no dynamic_cast, no reinterpret_cast</strong></li>
  </ul>
  
  <p>Disadvantages:</p>
  
  <ul>
  <li>Every time a new singleton type appears, a <strong>getSingleton</strong> specialization, doing the same - i.e. "<strong>return s_singletons;</strong>" must be added to <strong>singleton_factory.cpp</strong></li>
  </ul>
  
  <p>So, as far as I can see, this is <strong>actually fairly good</strong> so I'm thinking to leave it like this, but I'm <strong>asking for your feedback</strong> (what better place to do that than the programming community?).</p>
  
  <p>What <strong>extra advantages/disadvantages</strong> do you see with this solution?</p>
  
  <p>What <strong>alternatives</strong> do you suggest?</p>