How can I create a factory function template that constructs object given an arbitrary number of arguments?
<p>This is easier to explain with some code so I'll give an example first:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

class Base {
public:
    int integer;

    Base() : integer(0) {}
    Base(int i) : integer(i) {}
};

class Double: public Base {
public:
    Double(int i) { integer = i * 2; }
};

class Triple: public Base {
public:
    Triple(int i) { integer = i * 3; }
};

template&lt;typename T&gt;
Base* createBaseObject(int i) {
    return new T(i);
};

int main() {
    std::vector&lt;Base*&gt; objects;

    objects.push_back(createBaseObject&lt;Double&gt;(2));
    objects.push_back(createBaseObject&lt;Triple&gt;(2));

    for(int i = 0; i &lt; objects.size(); ++i) {
        std::cout &lt;&lt; objects[i]-&gt;integer &lt;&lt; std::endl;
    }

    std::cin.get();
    return 0;
}
</code></pre>

<p>I am trying to make a function that will return a <code>Base</code> pointer to an object that is derived from <code>Base</code>.  In the above code the function <code>createBaseObject</code> allows me to do that but it restricts me in that it can only create dervied classes that take a single argument into their constructor.</p>

<p>For example if I wanted to make a derived class Multiply:</p>

<pre><code>class Multiply: public Base {
public:
    Multiply(int i, int amount) { integer = i * amount; }
};
</code></pre>

<p><code>createBaseObject</code> wouldn't be able to create a <code>Multiply</code> object as it's constructor takes two arguments.</p>

<p>I want to ultimately do something like this:</p>

<pre><code>struct BaseCreator {
    typedef Base* (*funcPtr)(int);
    BaseCreator(std::string name, funcPtr f) : identifier(name), func(f) {}

    std::string identifier;
    funcPtr func;
};
</code></pre>

<p>then, for example, when I get input matching <code>identifier</code> I can create a new object of whatever derived class associates with that identifier with whatever arguments were input too and push it to the container.</p>

<hr>

<p>After reading some of the replies I think something like this would suit my needs to be able to procedurally create an instance of an object? I'm not too wise with templates though so I do not know whether this is legal.</p>

<pre><code>struct CreatorBase {
    std::string identifier;
    CreatorBase(std::string name) : identifier(name) {}

    template&lt;typename... Args&gt;
    virtual Base* createObject(Args... as) = 0;
};

template&lt;typename T&gt;
struct Creator: public CreatorBase {
    typedef T type;

    template&lt;typename... Args&gt;
    Base* createObject(Args... as) {
        return new type(as...);
    }
};
</code></pre>

<hr>

<p>Okay here's another semi-solution I've managed to come up with so far:</p>

<pre><code>#include &lt;boost\lambda\bind.hpp&gt;
#include &lt;boost\lambda\construct.hpp&gt;
#include &lt;boost\function.hpp&gt;

using namespace boost::lambda;
boost::function&lt;Base(int)&gt; dbl = bind(constructor&lt;Double&gt;(), _1);
boost::function&lt;Base(int, int)&gt; mult = bind(constructor&lt;Multiply&gt;(), _1, _2);
</code></pre>

<p>Just this has the same limits as the original in that I can't have a single pointer that will point to both <code>dbl</code> and <code>mult</code>.</p>
