title: C++ generic factory with multiple constructor signatures?
body: <p>Has anyone ever combined the classic generic factory by Andrei Alexandrescu (page 208 of <a href="http://ptgmedia.pearsoncmg.com/images/0201704315/samplechapter/alexan08.pdf" rel="nofollow noreferrer">Chapter 8</a> in <a href="http://erdani.com/index.php/books/modern-c-design/" rel="nofollow noreferrer">Modern C++ Design</a>) with the 'multifunction' capabilities of <a href="http://www.boost.org/doc/libs/1_66_0/doc/html/boost_typeerasure/examples.html#boost_typeerasure.examples.multifunction" rel="nofollow noreferrer">Boost.TypeErasure</a>? That is, the flexibility to have several creator function signatures that vary with respect to number and type of parameters (but still have the same return type and are known at compile time).</p>
  
  <p>In other words, how to combine this slightly simplified generic Factory:</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;utility&gt;
  #include &lt;stdexcept&gt;
  
  template &lt;class AbstractProduct, typename IdentifierType, typename ProductCreator&gt;
  class Factory
  {
  public:
      bool Register(const IdentifierType&amp; id, ProductCreator creator) {
          return associations_.emplace(id, creator).second;
      }
  
      bool Unregister(const IdentifierType&amp; id) {
          return associations_.erase(id) == 1;
      }
  
      template &lt;typename... Arguments&gt;
      AbstractProduct CreateObject(const IdentifierType&amp; id, Arguments&amp;&amp; ... args) {
          auto i = associations_.find(id);
          if (i != associations_.end()) {
              return (i-&gt;second)(std::forward&lt;Arguments&gt;(args)...);
          }
          throw std::runtime_error("Creator not found.");
      }
  
  private:
      std::map&lt;IdentifierType, ProductCreator&gt; associations_;
  };
  </code></pre>
  
  <p>with this (incomplete) function type erasure 'pattern':</p>
  
  <pre><code>#include &lt;boost/type_erasure/any.hpp&gt;
  #include &lt;boost/type_erasure/builtin.hpp&gt;
  #include &lt;boost/type_erasure/callable.hpp&gt;
  #include &lt;boost/mpl/vector.hpp&gt;
  #include &lt;boost/variant.hpp&gt;    
  
  template&lt;class... Sig&gt;
  using multifunction = any&lt; mpl::vector&lt; copy_constructible&lt;&gt;, typeid_&lt;&gt;, relaxed, callable&lt;Sig&gt;... &gt; &gt;;
  using variant_type = boost::make_recursive_variant&lt; void, double, ... &gt;::type;
  using function_type = multifunction&lt;AbstractProduct(void), AbstractProduct(double), AbstractProduct(double, double)&gt;;
  
  class variant_handler
  {
  public:
      void handle(const variant_type&amp; arg) {
          boost::apply_visitor(impl, arg);
      }
      void set_handler(function_type f) {
          impl.f = f;
      }
  private:
      struct dispatcher : boost::static_visitor&lt;void&gt;
      {
          template&lt;class T&gt;
          void operator()(const T&amp; t) { f(t); }
          // For a vector, we recursively operate on the elements
          void operator()(const vector_type&amp; v)
          {
              boost::for_each(v, boost::apply_visitor(*this));
          }
          function_type f;
      };
      dispatcher impl;
  };
  </code></pre>
  
  <p>So that ultimately one can use it like:</p>
  
  <pre><code>Factory&lt;Arity*, int, ???&gt; factory;
  factory.Register(0, boost::bind( boost::factory&lt;Nullary *&gt;() ));
  factory.Register(1, boost::bind( boost::factory&lt;Unary *&gt;(), _1 ));
  auto x = factory.CreateObject(0);
  auto y = factory.CreateObject(1, 0.5);
  </code></pre>
  
  <p>I haven't found an existing implementation in the wild, and I am currently stuck in my own attempt to make it. My first attempt made the mistake of trying to store the result of <code>boost::bind()</code> in the <code>function_type</code>, which resulted the same error to <a href="https://stackoverflow.com/questions/5331385/problem-with-boostbind-boostfunction-and-boostfactory">this</a> SO question. I suspect the answer will require moving the <code>ProductCreator</code> template parameter to the <code>Register</code> function and doing something there.</p>
  
  <p>So I guess I am ultimately looking for a full, working implementation of a generic multifunction factory, which may already exist and I just overlooked it. But any help with just getting it together would be really appreciated.</p>
  
  <p>I would prefer a C++11 solution, but obviously C++14 is better than none, etc.</p>
  
  <p>Thanks in advance for any help with this!</p>