title: How to self register class instances using the CRTP?
body: <p>I have a registry for lambda functions associated with a specific <code>CommandId</code>, where the registered function is supposed to create a concrete instance of a command executor class and supply it with some data:</p>
  
  <h3>CommandId.h</h3>
  
  <pre><code>#include &lt;cstdint&gt;
  
  enum class CommandId : uint16_t {
      Command1 ,
      Command2 ,
  };
  </code></pre>
  
  <h3>Registry.h</h3>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;functional&gt;
  #include &lt;string&gt;
  
  #include "CommandId.h"
  
  class Registry
  {
  public:
      static std::map&lt;CommandId,std::function&lt;void (std::string)&gt;&gt;&amp; 
      GetCommands() {
          static std::map &lt; CommandId, std::function&lt;void(std::string)&gt;&gt; 
          theFunctionRegistry;
          return theFunctionRegistry;
      }
  };
  </code></pre>
  
  <p>Now my idea was to provide concrete command implementations as derived classes from a CRTP template base, that provides a <code>static</code> member of itself just for the purpose of registering an associated lambda function (because the workflow for calling the derived classes will be just boilerplate code):</p>
  
  <h3>CommandBase.h</h3>
  
  <pre><code>#include "CommandId.h"
  #include "Registry.h"
  
  // The general command execution interface
  struct ICommand {
      virtual void Execute(const std::string&amp; data) = 0;
      virtual ~ICommand() {}
  };
  
  template&lt;typename Derived, CommandId CmdId&gt;
  class CommandBase : public ICommand
  {
  public:
      virtual ~CommandBase() {}
      void Register() {}
  protected:
      // Dummy function to avoid abstract instantiations, should probably throw
      void Execute(const std::string&amp; data) override {
      }
  
      // Protected constuctor meant for concrete command classes
      CommandBase(int&amp; derivedRef) : derivedRef_(&amp;derivedRef) {}
  
      // The static member that should perform the registation automagically
      static CommandBase&lt;Derived, CmdId&gt; registryAdder_;
  
      // This constructor is meant to be accessed from the above static member
      // instantiation only, and just register the lambda function 
      CommandBase() : derivedRef_(nullptr) {
          // Register a lambda function that ususe a concrete Derived instance
          Registry::GetCommands()[CmdId] = [](const std::string&amp; data) {
              Derived derivedInstance;
  
              CommandBase&lt;Derived, CmdId&gt;* der = static_cast&lt;CommandBase&lt;Derived, CmdId&gt;*&gt;(&amp;derivedInstance);
              // Manipulate the derived instances data and execute
              *(der-&gt;derivedRef_) = 42;
              der-&gt;Execute(data);
          };
      }
  
      // Provides access to the derived class instances data members and allows manipulation 
      int* derivedRef_;
  };
  
  template&lt;typename Derived, CommandId CmdId&gt;
  CommandBase&lt;Derived, CmdId&gt; CommandBase&lt;Derived, CmdId&gt;::registryAdder_;
  </code></pre>
  
  <p>I thought that accessing the <code>static registryAdder_</code> member of the base would force the compiler to provide an instantiation, but that doesn't happen.</p>
  
  <p>Here's a trial for <code>Command1</code> and <code>Command2</code></p>
  
  <pre><code>&lt;h3&gt;Command1.h&lt;/h3&gt;
  
  #include "CommandBase.h"
  
  class Command1 : public CommandBase&lt;Command1, CommandId::Command1&gt;
  {
  public:
      typedef CommandBase&lt;Command1, CommandId::Command1&gt; BaseClass;
      friend class BaseClass;
  
  public:
      Command1(CommandId) {
          BaseClass::registryAdder_.Register();
      }
      virtual ~Command1() override {}
  
  private:
      Command1() : BaseClass(member_)
      {
          BaseClass::registryAdder_.Register();
      }
  
      void Execute(const std::string&amp; data) override {
  
      }
  private:
      int member_;
  };
  </code></pre>
  
  <h3>Command2.h</h3>
  
  <pre><code>#include "CommandBase.h"
  
  class Command2 : public CommandBase&lt;Command2, CommandId::Command2&gt;
  {
  public:
      typedef CommandBase&lt;Command2, CommandId::Command2&gt; BaseClass;
      friend class BaseClass;
  
  public:
      Command12CommandId) {
          BaseClass::registryAdder_.Register();
      }
      virtual ~Command1() override {}
  
  private:
      Command2() : BaseClass(member_)
      {
          BaseClass::registryAdder_.Register();
      }
  
      void Execute(const std::string&amp; data) override {
  
      }
  private:
      int member_;
  };
  </code></pre>
  
  <p>I was hoping that the line</p>
  
  <pre><code>BaseClass::registryAdder_.Register();
  </code></pre>
  
  <p>in the constructors would force the instantiation of the <code>BaseClass::registryAdder_</code> <code>static</code> base class member. But it obviously doesn't, and the compiler just strips it away:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Command1.h"
  #include "Command2.h"
  #include "Registry.h"
  
  int main()
  {
      std::cout &lt;&lt; "There are " &lt;&lt; Registry::GetCommands().size() &lt;&lt; " registered commands." &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>There are 0 registered commands.
  </code></pre>
  
  <hr>
  
  <p>Now my question is:</p>
  
  <p><strong>How can I force the compiler to instantiate those <code>static</code> base class members from the <code>CommandBase</code> template class?</strong></p>
  
  <hr>
  
  <p>I've done some research of course, but these Q&amp;A's didn't really satisfy me:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/6420985/how-to-force-a-static-member-to-be-initialized">How to force a static member to be initialized?
  </a></li>
  <li><a href="https://stackoverflow.com/questions/43799201/force-template-static-member-instantiation">Force template static member instantiation
  </a></li>
  <li><a href="https://stackoverflow.com/questions/27964543/explicit-template-static-member-instantiation-in-a-derived-class">Explicit template static member instantiation in a derived class
  </a></li>
  <li>... and maybe more</li>
  </ul>