title: How to define a variant<x,y,z> extracting subtypes of a template parameter
body: <p>I am building a state-machine where state transitions are described as a variant, i.e.:</p>
  
  <pre><code>using table = std::variant&lt;
  /*             state        event               followup-state    */
      transition&lt;start,       success&lt;sock&gt;,      connecting&gt;,
      transition&lt;start,       exception,          failed&gt;,
      transition&lt;connecting,  success&lt;&gt;,          connected&gt;,
      transition&lt;connecting,  exception,          failed&gt;,
      transition&lt;connected,   exception,          failed&gt;
      &gt;;
  </code></pre>
  
  <p>and transition being a simple type:</p>
  
  <pre><code>template &lt;typename ENTRY_STATE, typename EVENT, typename NEXT_STATE&gt;
  struct transition {
      using entry_state = ENTRY_STATE;
      using event = EVENT;
      using next_state = NEXT_STATE;
  };
  </code></pre>
  
  <p>The state classes are non-polymorphic (and shall not be). My question now is how to define another variant that is able to store all possible states found in the table type (best without duplicates). The type is needed to store the actual state in a type-safe and non-polymorphic way.</p>
  
  <p>From the above table, we have a unique set of entry states:</p>
  
  <pre><code>entry_states = &lt;start,connecting,connected&gt;
  </code></pre>
  
  <p>and a set of follow-up states:</p>
  
  <pre><code>followup_states = &lt;connecting, connected, failed&gt;
  </code></pre>
  
  <p>so the resulting variant definition shall be a:</p>
  
  <pre><code>using states = std::variant&lt;entry_states JOINT followup_states&gt;;
  
  =&gt;  using states = std::variant&lt;start,connecting,connected, failed&gt;
  </code></pre>
  
  <p>I know how to extract type information from the table and access type information of a particular transition, but have no idea how to transfer possible states into a variant definition (without duplicate types).</p>
  
  <p>Any idea is appreciated. However, polymorphism is not a valid solution. Also saving the current state inside a lambda is not an option.</p>
  
  <p>Thanks and best!</p>
  
  <p>PS: the state-machine signature looks like that (I am not posting the full code since it is not meaningful for the question, imo):</p>
  
  <pre><code>template &lt;typename TransitionTable, typename Context&gt;
  class state_machine {
  public:
      template &lt;typename State, typename Event&gt;
      auto push(State &amp; state, Event &amp; event) {
      ...
      }
  protected:
      *using states = std::variant&lt;???&gt;;*
      states current_state;
  };
  </code></pre>