模板模板参数用作其他参数的默认值
Template template parameters used as default value for other arguments
我试图将模板模板参数用作其他模板参数的默认值,但是当我尝试使用标识符(模板模板参数名称/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;
}
相关文章:
- 当给定默认值时,为什么此模板参数推导失败
- 如何在C++中提供模板化函数作为另一个函数的参数,默认值?
- 在 c++ 中,如果我创建一个接受一个具有默认值的参数的构造函数 - 它会用作默认(空)构造函数吗?
- 为什么无法在可变参数模板中将尾随模板参数指定为默认值?
- 可变参数模板和具有继承的默认值
- 具有默认值的单个参数构造函数是否与默认构造函数相同?
- 在子类函数覆盖中省略具有默认值的参数
- 函数作为具有默认值的参数
- 具有另一个具有默认值的模板参数的模板推导
- const变量是否可以在具有默认值的参数中赋值(作为可选参数)
- 可以通过常量引用返回默认参数的值吗?
- 对 const 引用参数使用默认值会导致崩溃
- C ,构造器中允许的参考参数默认值
- 是否可以设计一个包含模板参数默认值的类
- 构造函数中参数C++默认值
- 如何设置依赖于其他参数的参数默认值
- QInput对话框参数默认值
- 在函数定义中指定参数默认值会导致错误 C2143:语法错误:'='之前缺少')'
- 内置类型的模板参数默认值
- 是否可以使用构造函数或对象作为其他类方法的参数默认值?