有没有一种方法可以为每个可变参数模板参数自动创建一个方法?
例如,在下面的代码中,我想强制覆盖 class 中的
:void x(a &v)
和 void x(b &v)
我
#include <type_traits>
#include <stdlib.h>
#include <stdio.h>
class a {
};
class b {
};
template <typename ...T>
class t {
public:
virtual void x(T &v) = 0;
};
class i : public t<a, b>
{
};
int
main (int argc, char *argv[])
{
i ii;
return 0;
}
最佳答案
您可以创建一个 t_impl
来保存单个 T
的虚拟函数,例如
template <typename T>
class t_impl
{
public:
virtual void x(T &v) = 0;
};
然后t
会继承它,就像
template <typename ...T>
class t : t_impl<T>... // can use public, protected or private inheritance
{
public:
using t_impl<T>::x...; // used to import t_impl<T>::x into the public space
// any additional common members
};
关于c++ - 可变参数模板 : One method per template argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64580758/