简体   繁体   中英

Inheritance with variadic templates

I'm trying to use inheritance with variadic templates. First, consider the following snippet as a skeleton I'd like to build upon.

#include <type_traits>
#include <iostream>

template <typename K> class A1;   // fwd decl

template <typename K,
        template <typename> class NN = A1,
        class = typename std::enable_if< std::is_base_of< A1<K>, NN<K> >::value >::type >
class BB;

template <typename K>
class A1 { public: friend class BB<K>; };

template <typename K>
class A2 : public A1<K> {
};

template <typename K, template <typename> class NN>
class BB<K,NN> {
    NN<K>* ref;
public:
    BB() : ref{ new NN<K>{} } { std::cout << "ctor...\n"; };
};

int main() {
    BB<size_t> b1{};    //use default A1
    BB<size_t, A2> b2{};
}

What we have here is that I can use the class BB with any class inherited from A1 that has exactly 1 template parameter.

Question: I'd like to modify this pattern in such a way that the template parameter NN can be any class inherited from A1<K> that may also have an arbitrary number of additional template parameters, how can I do it? The following snippet is the rough idea

template <typename K> class A1;   // fwd decl

template <typename K,
        template <typename, typename...> class NN = A1,
        class = typename std::enable_if< std::is_base_of< A1<K>, NN<K,typename...> >::value >::type,
        typename...Types >
class BB;

template <typename K>
class A1 { public: friend class BB<K>; };

template <typename K, typename V>
class A2 : public A1<K> {
    V local;
};

template <typename K,
        template <typename, typename...> class NN,
        typename...Types>
class BB<K,NN,void,Types...> {
    NN<K,Types...>* ref;
public:
    BB() : ref{ new NN<K,Types...>{} } { std::cout << "ctor...\n"; };
};

int main() {
    BB<size_t> b1{};    //use default A1
    BB<size_t, A2, char> b2{};  // can I specialize this way??
}

Is it possible to do? Any help is greatly appreciated. VS2017 or GCC is indifferent.

template <typename K> class A1;   // fwd decl

template<class...>struct types_t {};
template <class K0, class Types=types_t<>, template<class...>class NN=A1, class=void>
class BB;

template <typename K>
class A1 { public: friend class BB<K>; };

template <class K, class other>
class A2 : public A1<K> {
};

template<class K, class...Ks>
class A3 : public A1<K> {};

template<class K0, class...Ks, template<class...>class NN>
class BB<
    K0,
    types_t<Ks...>,
    NN,
    std::enable_if_t<
        std::is_base_of<
            A1<K0>,
            NN<K0, Ks...>
        >{}
    >
>{
    NN<K0, Ks...>* ref = nullptr;
public:
    BB() : ref{ new NN<K0, Ks...>{} } { std::cout << "ctor...\n"; };
};

int main() {
    BB<size_t> b1{};    //use default A1
    BB<size_t, types_t<char>, A2> b2{};
    BB<size_t, types_t<int, char>, A3> b3{};
}

this compiles.

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