Class constructor with non-argument template type

c++, templates

Solution

No, there is no way to do that. The note at `14.8.1/5` in the Standard explains why

[Note: because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. ]

Of course, it doesn't need to be a `T` object you send. It can be any object that has `T` encoded in its type

template<typename T> struct type2type { };

struct factory {
    template<typename T>
    factory(type2type<T>)
        : func(allocate<T>)
    {}

    std::tr1::function<Base*()> func;
};

factory f((type2type<Foo>()));

Problem

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?

Original source