[英]Extract type from variadic template class for member function overloading
I would like to have a function overload for each type of a variadic template class.我想为每种类型的可变参数模板类都有一个函数重载。 Is that possible ?
那可能吗 ?
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
}
I tried using fold expressions but I'm pretty sure I'm not on the right track.我尝试使用折叠表达式,但我很确定我没有走在正确的轨道上。 Because I need the functions to be virtual, I cannot declare them as
template<typename T> virtual void doSomething(T arg)
because, well, template virtual functions aren't allowed.因为我需要函数是虚拟的,所以我不能将它们声明为
template<typename T> virtual void doSomething(T arg)
因为,好吧,模板虚拟函数是不允许的。
You can derive the class from a templated pack of instantiations of a base class template that defines one virtual function with its single template parameter as the function parameter type.您可以从基类模板的模板化实例化包派生该类,该基类模板定义了一个具有单个模板参数作为函数参数类型的虚函数。 The derived class then holds the function overloads for each template argument type, that are all virtual.
派生类然后保存每个模板参数类型的函数重载,它们都是虚拟的。
template<typename arg>
class Base {
virtual void doSomething(arg arg) {}
};
template<typename ...args>
class Example : public Base<args>... {};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.