简体   繁体   中英

Is it possible for template-class to inherit from multiple parents based on parameter pack?

I'd like to achieve something like this:

class C: public Wrapper<C1>, public Wrapper<C2>, public Wrapper<C3> ..... {}

...but pass any number of Cx as variadic template argument to class C:

template<typename... Cx>
class C: // ???????????????

Is it possible? Is there any workaroud for this?

Yes this is possible:

struct A {};
struct B {};

template <typename ...P>
struct C : P... {};

int main() {
    C<A,B> c;
}

or with a wrapper:

template <typename T>
struct Wrapper {};

template <typename ...P>
struct C : Wrapper<P>... {};

Here you can find an example where the same is applied in the context of std::visit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM