title: Singletons, curiously recurring template pattern and forwarding constructor parameters
body: <p>Ok, I know Singletons should be avoided, however there are few instances where one really needs them. So my solution implements them using the CRTP (curiously recurring pattern), as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;utility&gt;
  
  using namespace std;
  
  template&lt;typename T&gt; // Singleton policy class
  class Singleton
  {
  protected:
      Singleton() = default;
      ~Singleton() = default;
      Singleton(const Singleton&amp;) = delete;
      Singleton&amp; operator=(const Singleton&amp;) = delete;
  public:
      template&lt;typename... Args&gt;
      static T&amp; getInstance(Args... args) // Singleton
      {
          // Guaranteed to be destroyed.
          // Instantiated on first use.
          // Thread safe in C++11
          static T instance{std::forward&lt;Args&gt;(args)...};
          return instance;
      }
  };
  
  class Foo: public Singleton&lt;Foo&gt;
  {
      friend class Singleton&lt;Foo&gt;;
      Foo()
      {
          cout &lt;&lt; "Constructing instance " &lt;&lt; this &lt;&lt;" of Foo" &lt;&lt; endl;
      }
      Foo(int x)
      {
          cout &lt;&lt; "Constructing instance " &lt;&lt; this &lt;&lt;" of Foo with argument x = "\
               &lt;&lt; x &lt;&lt; endl;
      }
      ~Foo()
      {
          cout &lt;&lt; "Destructing instance " &lt;&lt; this &lt;&lt; " of Foo" &lt;&lt; endl;   
      }
  public:
      // public 
  };
  
  int main()
  {
      Foo&amp; rfoo = Foo::getInstance(); // default constructible
  
      // this should just return the instance
      // instead, it constructs another instance
      // because invokes an overloaded version of get_instance()  
      Foo&amp; rfoo1 = Foo::getInstance(1); 
  
      // this is OK
      // calls the SAME overloaded version again, instance is static
      // so we get the same instance
      Foo&amp; rfoo2 = Foo::getInstance(2); 
  }
  </code></pre>
  
  <p>As you see, I allow the possibility of creating singletons out of classes with overloaded/non-defaulted constructors. But this comes and bites me back, because, by using a variadic-templated <code>get_instance()</code> function and passing the instance parameters via <code>std::forward</code>, the compiler generates an overload for each call with different types, and therefore returns a new static instance for each overload. What I would like is to return by reference a single instance common somehow to all possible overloads, but cannot figure out how to do it. Any ideas how to do it? Thanks!</p>
  
  <p>PS: I can implement a solution with pointers instead of references, but I prefer the reference solution as it is thread safe and more elegant in my opinion.</p>