元生成的可变参数接口实现

问题描述 投票:0回答:1

我已经用模板化可变参数列表定义了接口,以具有与每种类型匹配的多种方法:

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纯方法不匹配。

任何想法我该如何解决?

c++ metaprogramming variadic-templates
1个回答
0
投票

我已经弄清楚了,需要虚拟继承来匹配那些接口。

编译代码:

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;
© www.soinside.com 2019 - 2024. All rights reserved.