0

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.

4
  • NN isn't a class, it is a template class. These are as different as a class and an variable; I don't know if your use of NN as a class betrays a misunderstanding of terminology or a misunderstanding of your problem. Second, your first code block doesn't look like it should work as it should require a ,void, but somehow it compiles. I'm puzzled. Commented May 22, 2018 at 17:15
  • yeah template class ofc. Where exactly did you expect a void? Commented May 22, 2018 at 18:48
  • BB<K,NN> I'd expect BB<K,NN,void>. I guess that you don't have to repeat defaulted template parameters in specializations or something? Commented May 22, 2018 at 19:10
  • Yeah that's the reason. In particular because it's the last one. Commented May 22, 2018 at 19:25

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.