title: std::map<std::variant, std::variant> with mapping from KeyType to ValueType
body: <p>I'm trying to create a <code>std::map</code> of variant to variant with a mapping from the <code>KeyType</code> to the <code>MappedType</code>.</p>
  
  <p>This works for up to around 100 key types or so: <a href="https://coliru.stacked-crooked.com/a/3959534e4fa38caa" rel="nofollow noreferrer">https://coliru.stacked-crooked.com/a/3959534e4fa38caa</a>. When I attempt to compile with 200 key types, GCC thinks hard for a while and then gives up with the error:</p>
  
  <pre><code>g++: internal compiler error: Killed (program cc1plus)
  </code></pre>
  
  <p>I believe it may be a problem with the parameter pack being too large. Is there a more scalable solution?</p>
  
  <p>I include below the salient parts of the solution:</p>
  
  <pre><code>template&lt;typename Key, typename T&gt;
  struct TypeMap {
      using KeyType = Key;
      using MappedType = T;
  };
  
  template&lt;typename&gt;
  constexpr bool false_v = false;
  
  // End case
  template&lt;typename Key, typename...&gt;
  struct MappedTypeForKey {
      static_assert(false_v&lt;Key&gt;, "Container mapping for key type not found");
  };
  
  // Recursive case
  template&lt;typename Key, typename MapKey, typename T, typename... TypeMaps&gt;
  struct MappedTypeForKey&lt;Key, TypeMap&lt;MapKey, T&gt;, TypeMaps...&gt; : MappedTypeForKey&lt;Key, TypeMaps...&gt; {
  };
  
  // Match case
  template&lt;typename Key, typename T, typename... TypeMaps&gt;
  struct MappedTypeForKey&lt;Key, TypeMap&lt;Key, T&gt;, TypeMaps...&gt; {
      using Type = T;
  };
  
  template&lt;typename... TypeMaps&gt;
  struct VariantTypeMapImpl {
      using KeyType = std::variant&lt;typename TypeMaps::KeyType...&gt;;
      using MappedType = std::variant&lt;typename TypeMaps::MappedType...&gt;;
      template&lt;typename Key&gt;
      using TypeForKey = typename MappedTypeForKey&lt;Key, TypeMaps...&gt;::Type;
  };
  
  // This is the key part of the code, allowing a developer to extend the map variants
  // at the same time as defining the type mappings:
  using VariantTypeMap = VariantTypeMapImpl&lt;
      TypeMap&lt;FrogKey, Frog&gt;,
      TypeMap&lt;CatKey, Cat&gt;
  &gt;;
  
  class VariantMap {
    public:
      size_t size() const { return map_.size(); }
  
      template&lt;typename Key&gt;
      void insert(Key key, VariantTypeMap::TypeForKey&lt;Key&gt; value)
      {
          map_.emplace(std::move(key), std::move(value));
      }
  
      template&lt;typename Key&gt;
      const VariantTypeMap::TypeForKey&lt;Key&gt;&amp; get(const Key&amp; key) const
      {
          return std::get&lt;VariantTypeMap::TypeForKey&lt;Key&gt;&gt;(map_.at(key));
      }
  
    private:
      std::map&lt;VariantTypeMap::KeyType, VariantTypeMap::MappedType&gt; map_;
  };
  </code></pre>