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.
NNisn't aclass, it is atemplate class. These are as different as a class and an variable; I don't know if your use ofNNas aclassbetrays 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.template classofc. Where exactly did you expect a void?BB<K,NN>I'd expectBB<K,NN,void>. I guess that you don't have to repeat defaulted template parameters in specializations or something?