从可变参数模板 class 中提取类型以进行成员函数重载
Extract type from variadic template class for member function overloading
我想为每种类型的可变参数模板重载函数 class。这可能吗?
template<typename ...args>
class Example {
virtual void doSomething(args(0) arg) { ... }
virtual void doSomething(args(1) arg) { ... }
/// etc... implementations are the same, but I need access to the type
}
我试过使用折叠表达式,但我很确定我的方向不对。因为我需要函数是虚拟的,所以我不能将它们声明为 template<typename T> virtual void doSomething(T arg)
因为模板虚拟函数是不允许的。
您可以从基本 class 模板的实例化模板包派生 class,该模板定义一个虚函数,其单个模板参数作为函数参数类型。
派生的 class 然后保存每个模板参数类型的函数重载,它们都是虚拟的。
template<typename arg>
class Base {
virtual void doSomething(arg arg) {}
};
template<typename ...args>
class Example : public Base<args>... {};
我想为每种类型的可变参数模板重载函数 class。这可能吗?
template<typename ...args>
class Example {
virtual void doSomething(args(0) arg) { ... }
virtual void doSomething(args(1) arg) { ... }
/// etc... implementations are the same, but I need access to the type
}
我试过使用折叠表达式,但我很确定我的方向不对。因为我需要函数是虚拟的,所以我不能将它们声明为 template<typename T> virtual void doSomething(T arg)
因为模板虚拟函数是不允许的。
您可以从基本 class 模板的实例化模板包派生 class,该模板定义一个虚函数,其单个模板参数作为函数参数类型。 派生的 class 然后保存每个模板参数类型的函数重载,它们都是虚拟的。
template<typename arg>
class Base {
virtual void doSomething(arg arg) {}
};
template<typename ...args>
class Example : public Base<args>... {};