title: C++ self resolving class
body: <p>I am facing an issue of creating a wrapper for a kind of complicated data structure. Let's say I have messages which have some features which are shared, and absolutely different "detail" kind of fields, which are to be stored in the same struct. I have created two classes, one of them is "Base" of structure and the second is handling additional data inside of those "special" fields. Let me show a code that I am working on.</p>
  
  <p>Here is a <code>Message.hxx</code>:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>namespace mgm = meta::game::message;
  
  namespace shared::common::message
  {
      class Message
      {
          std::unique_ptr&lt;mgm::Message&gt; mp_data;
  
      public:
          [[nodiscard]] std::uint64_t getMessageId() { return mp_data-&gt;id(); }
  
          [[nodiscard]] std::uint8_t getSender() { return mp_data-&gt;sender(); }
          [[nodiscard]] std::uint8_t getRecipient() { return mp_data-&gt;recipient(); }
  
          [[nodiscard]] std::string getInfo() { return mp_data-&gt;info()-&gt;str(); }
  
          [[nodiscard]] std::uint64_t getServerTimeStamp() { return mp_data-&gt;server_time_stamp(); }
      };
  }
  
  namespace scm = shared::common::message;
  </code></pre>
  
  <p>Here is the "detail" class which is not really implemented, but it is not important <code>SimpleMessage.hxx</code>:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>namespace shared::common::message
  {
      class SimpleMessage
          : public Message
      {
  
      };
  }
  </code></pre>
  
  <p>I was thinking, </p>
  
  <blockquote>
    <p>Hey maybe I will add template type deduction/specialization! It sounds like a great idea!</p>
  </blockquote>
  
  <p><code>MessageResolver.hxx</code></p>
  
  <pre class="lang-cpp prettyprint-override"><code>namespace
  {
      namespace mgm = meta::game::message;
  }
  
  namespace shared::common::message
  {
      class Message;
      class SimpleMessage;
  
      template&lt;mgm::Request RESPONSE_TYPE, mgm::Response REQUEST_TYPE&gt;
      class MessageResolver : std::false_type {};
  
      template&lt;&gt;
      struct MessageResolver&lt;mgm::Request::Request_SimpleRequest, mgm::Response_SimpleResponse&gt;
      {
          SimpleMessage* operator ()(Message* message)
          {
              return reinterpret_cast&lt;SimpleMessage*&gt;(message);
          }
      };
  }
  </code></pre>
  
  <p>and add code to my <code>Message.hxx</code> class...</p>
  
  <pre class="lang-cpp prettyprint-override"><code>namespace mgm = meta::game::message;
  
  namespace shared::common::message
  {
      class Message
      {
          std::unique_ptr&lt;mgm::Message&gt; mp_data;
  
      public:
          [[nodiscard]] std::uint64_t getMessageId() { return mp_data-&gt;id(); }
  
          [[nodiscard]] std::uint8_t getSender() { return mp_data-&gt;sender(); }
          [[nodiscard]] std::uint8_t getRecipient() { return mp_data-&gt;recipient(); }
  
          [[nodiscard]] std::string getInfo() { return mp_data-&gt;info()-&gt;str(); }
  
          [[nodiscard]] std::uint64_t getServerTimeStamp() { return mp_data-&gt;server_time_stamp(); }
  
  
  
          [[nodiscard]] decltype(MessageResolver&lt;req,resp&gt;()) get() { return MessageResolver&lt;req,resp&gt;(); };
  
  
      };
  }
  
  namespace scm = shared::common::message;
  </code></pre>
  
  <p>and at this point, my dreams crumbled. I can't initialize this statement with non-<code>constexpr</code> types... </p>
  
  <p>My desired effect is, to get access to get a self-resolving class. Is this possible? I came here for the Gods of StackOverflow to assist, with my journey to find the answers.</p>