title: Visit and invoke a variant of std::function
body: <p>I'm using a <a href="http://en.cppreference.com/w/cpp/utility/variant" rel="nofollow noreferrer"><code>std::variant</code></a> to store different signatures of <a href="http://en.cppreference.com/w/cpp/utility/functional/function" rel="nofollow noreferrer"><code>std::function</code></a>s. Said functions are stored in a vector, the latter being retrieved from a map. </p>
  
  <p>How do I invoke each function in a vector of <a href="http://en.cppreference.com/w/cpp/utility/variant" rel="nofollow noreferrer"><code>std::variant</code></a>? I feel like I should use <a href="http://en.cppreference.com/w/cpp/utility/variant/visit" rel="nofollow noreferrer"><code>std::visit</code></a> but I can't figure out the correct usage.</p>
  
  <pre><code>#include &lt;variant&gt;
  #include &lt;functional&gt;
  #include &lt;map&gt;
  #include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  using var_t = std::variant&lt;std::function&lt;void(void)&gt;, std::function&lt;void(int)&gt;&gt;;
  enum class EventEnum { A, B, C };
  
  struct Controller {
      template&lt;EventEnum E&gt;
      void subscribe(var_t fn) {
          auto&amp; callbacksVec = callbacks.at(E);
          callbacksVec.push_back(fn);
      }
  
      template&lt;EventEnum E&gt;
      void notify() {
          auto&amp; callbacksVec = callbacks.at(E);
          for (auto&amp; func : callbacksVec) {
              // std::visit([](auto&amp; arg){ std::invoke(arg); }, func);
          }
      }
  
      std::map&lt;EventEnum, std::vector&lt;var_t&gt;&gt; callbacks;
  };
  
  int main() {
      auto fn = []() { std::cout &lt;&lt; "lambda callback" &lt;&lt; std::endl; };
      Controller myController;
      myController.subscribe&lt;EventEnum::A&gt;(fn);
      myController.notify&lt;EventEnum::A&gt;();
  
      return 0;
  }
  </code></pre>