我已经用模板化可变参数列表定义了接口,以具有与每种类型匹配的多种方法:
template <typename T> struct IfaceElement { virtual void m(const T &) = 0; };
template <typename... Ts> struct Iface : IfaceElement<Ts>... {};
我想实现的是为不同数量的Event
对象提供一个接口。类似于:
struct A {};
struct B {};
struct Handler : Iface<A, B> {
void m(const A &) override { ... }
void m(const B &) override { ... }
}
并且效果很好。
但是,我也想有一个实现该接口的类,它也是从可变参数模板中元生成的。
我认为这样的事情应该起作用:
template <typename T> struct IfaceElement { virtual void m(const T &) = 0; };
template <typename... Ts> struct Iface : IfaceElement<Ts>... {};
template <typename T> struct ImplElement : IfaceElement<T> {
void m(const T &) override {}
};
template <typename... Ts> struct Impl : Iface<Ts...>, ImplElement<Ts>... {};
struct Z {};
struct X {};
Impl<Z, X> q;
但是我遇到了编译器错误:
test.cpp:121:12: error: cannot declare variable 'q' to be of abstract type 'Impl<Z, X>'
121 | Impl<Z, X> q;
| ^
test.cpp:116:34: note: because the following virtual functions are pure within 'Impl<Z, X>':
116 | template <typename... Ts> struct Impl : Iface<Ts...>, ImplElement<Ts>... {};
| ^~~~
test.cpp:110:58: note: 'void IfaceElement<T>::m(const T&) [with T = X]'
110 | template <typename T> struct IfaceElement { virtual void m(const T &) = 0; };
| ^
test.cpp:110:58: note: 'void IfaceElement<T>::m(const T&) [with T = Z]'
[我的ImplElement
实现似乎与IfaceElement
纯方法不匹配。
任何想法我该如何解决?
我已经弄清楚了,需要虚拟继承来匹配那些接口。
编译代码:
template <typename T> struct IfaceElement { virtual void m(const T &) = 0; };
template <typename... Ts> struct Iface : virtual IfaceElement<Ts>... {};
template <typename T> struct ImplElement : virtual IfaceElement<T> {
void m(const T &) override {}
};
template <typename... Ts> struct Impl : Iface<Ts...>, ImplElement<Ts>... {};
struct Z {};
struct X {};
Impl<Z, X> q;