For a normal C++ function, it's possible to have template parameters not appearing in the argument list:
template<typename T>
T default_construct()
{
return T();
}
and call this with
some_type x = default_construct<some_type>();
Even though the type I'm using is not in the argument list, I can still pass it to the function. Now, I want to do this in a class constructor:
struct Base;
template<typename T>
Base* allocate()
{
return new T; //Assume T derives from Base...
}
struct factory {
template<typename T>
factory()
: func(allocate<T>)
{}
std::tr1::function<Base*()> func;
};
but I can't find a way to supply the parameter to the constructor when I want to construct an instance of factory
.
Is there a way to do this without turning the class into a templated class or sending some unused T
object to the constructor?