模板模板参数用作其他参数的默认值

Template template parameters used as default value for other arguments

本文关键字:参数 默认值 其他      更新时间:2023-10-16

我试图将模板模板参数用作其他模板参数的默认值,但是当我尝试使用标识符(模板模板参数名称/ID(时,找不到它。我正在使用VS 2013。这个想法是我有一个基于特定类型的模板实例化的"工厂"类,我需要返回一个具有相同数量参数(4(的对象,但具有相同的专业化。

template<class T1, class T2,class T3, class T4>
class CommandBus
{...}
template <
template<class T1, class T2, class T3, class T4> class ListenerType,
//The Line bellow fails to compile, T1 is not visible
//E0020 identifier "T1" is undefine 
class CommandBusType = CommandBus<T1, T2, T3, T4> >
class CommandBusFactory {
static auto Get() {
        return CommandBusType{};
    }
};
int main{
//Say I would Have some Object :
Listener<int, int ,int int> Listener;
//Withought the Factory I would have to manually generate a ComamndBus of the same specialization (int , int , int, int):
CommandBus<int,int,int,int> cmdBus;

//But I could use a factory, things should look like:
Listener<int, int ,int int> listener;
auto cmdBus = CommandBusFactory<Listener<int,int,int,int>>::Get();
}

我期望这能起作用,但编译器抱怨说标识符t1,t2等对于默认值(commandbus(的模板参数commandbustype

是否可以使用模板模板参数作为其他模板参数的默认值?

CommandBusFactory的第一个模板本身就是模板。

但是,在main中,您将Listener<int,int,int,int>传递给它,不是模板,而是类型。您需要一个类型的模板参数。

然后,要从该类型中提取模板参数,您可以使用模板专业化:

template
<
    class X,
    template <class, class, class, class> class Y
>
struct copy_template_params;
template
<
    template <class, class, class, class> class X,
    class A, class B, class C, class D,
    template <class, class, class, class> class Y
>
struct copy_template_params<X<A,B,C,D>,Y>
{
    using type = Y<A,B,C,D>;
};
template
<
    class ListenerType, 
    class CommandBusType = typename copy_template_params<ListenerType,CommandBus>::type
>
class CommandBusFactory
{
  public:
    static auto Get()
    {
        return CommandBusType{};
    }
};

目前尚不完全清楚您在这里尝试什么。使用模板的使用将允许CommandBusFactory创建提供的ListenerType的不同实例,而不是通过将其传递给:

template <
    template<class T1, class T2, class T3, class T4> class ListenerType,
    class CommandBusType
>
class CommandBusFactory {
    static auto Get() {
        ListenerType<int, int, float, float> listener; // template params go here
        ListenerType<std::string, std::string, double, double> listener2; // allowing different ones
        return CommandBusType{};
    }
};
int main() {
    CommandBusFactory<Listener, SomeCommandBus> factory; // not here
}

如果您确实要在前提供ListenerType,然后从中确定CommandBusType,则可以使用Typedefs,返回值或类似方式(例如具有value_type的容器等(。

template<class T1, class T2, class T3, class T4> class CommandBus {};
template<class T1, class T2, class T3, class T4> class Listener
{
public:
    typedef T1 t1;
    typedef T2 t2;
    typedef T3 t3;
    typedef T4 t4;
};
template <
    class ListenerType,
    class CommandBusType = CommandBus<typename ListenerType::t1, typename ListenerType::t2, typename ListenerType::t3, typename ListenerType::t4>
>
class CommandBusFactory {
    static auto Get() {
        return CommandBusType{};
    }
};
int main() {
    CommandBusFactory<Listener<int,int,float,float>> factory;
}