title: Alternative to conversion constructor for template parameter
body: <p>I am using GLFW for window and input management in a C++ application. In order to use a member function as a callback for key events, I used a singleton as suggested in an answer <a href="https://stackoverflow.com/questions/7676971/pointing-to-a-function-that-is-a-class-member-glfw-setkeycallback">here</a>.</p>
  
  <p>However, I need to implement different input handlers. My approach is to use the Singleton as a wrapper for the actual input handler (controller_) to allow for polymorphism. However, for the Singleton to be instantiated the base class cannot be abstract. The solution involved using the <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="nofollow noreferrer">CRTP</a> in order to be able to invoke the specific input handling method from the implementation of the base class.</p>
  
  <pre><code>  template &lt;class T&gt;
    class Controller : public BC{ //BC is just for using this class as a template parameter itself
      public:
      Controller(){}                    
      Controller(Controller &amp;controller){
        controller_ = &amp;controller;
      }
  
      static Controller&amp; getInstance(Controller *controller){
        static Controller instance(*controller);
        return instance;
      }   
  
      //This is the key move, where the concrete implementation is invoked.
      static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods){
        Controller *aux;
        (static_cast&lt;T&gt;(getInstance(aux))).keyCallbackImpl(window, key, scancode, action, mods); 
      }
  
      //Stub to be overridden by the concrete input handler        
      virtual void keyCallbackImpl(GLFWwindow* window, int key, int scancode, int action, int mods){}
  
      //This is the wrapped input handler
      Controller *controller_;   
  
    };
  </code></pre>
  
  <p>This works fine. However, there is a problem with the derived class. In order to be able to perform the cast, I must define a conversion constructor. </p>
  
  <pre><code>SMController(Controller&lt;SMController&gt; &amp;c){
    controller_ = c.controller_;
    std::cout &lt;&lt; "Constructor" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>This is inconvenient for two reasons:</p>
  
  <ul>
  <li>Users deriving from Controller must explicitly define this constructor</li>
  <li>The construction of a new wrapper every time a key is pressed seems costly</li>
  </ul>
  
  <p>Is there an alternative to this conversion using this design?</p>
  
  <p><strong>EDIT:</strong>
  I ended up going with T.C.'s proposal, although with a slight difference. Since I needed subclasses with their own set of parameters, it's ideal to be able to provide them in a constructor. The separate call to initialize the singleton is error prone, because it could be done with the wrong template argument, or simply forgotten. </p>
  
  <p>In order to enable the instantiation of the specialized object with its parameters and its corresponding singleton in one call, I kept using the CRTP and I added this constructor to the base class:</p>
  
  <pre><code>Controller&lt;T&gt;(){
    T::getInstance((T*)this);
  }
  </code></pre>
  
  <p>Now, with just one call I get everything I need:</p>
  
  <pre><code>std::shared_ptr&lt;BaseController&gt; c(new SMController(params_, window_));
  </code></pre>