How to implement is_stl_vector

I want to specialize a template for the arguments of an STL vector template. Something like that:

// (1) template <typename T> class A { ... }; // (2) template <> class A<std::vector<> > { ... }; 

I don't care what the type of the vector element is. I would like to use it as follows:

 A<int> a1; // Will use the general specialization A<std::vector<int> > a2; // Will use the second specialization 

In general, I'm trying to define something similar to boost type properties. Sort of

 template <class T> struct is_stl_vector { // Will be true if T is a vector, false otherwise static const bool value = ...; }; 

I cannot use a template template (I think so), because it must also compile for types without templates. Is it even possible?

+4
source share
2 answers

You can simply specialize as follows:

 // (2) template <typename T, typename Alloc> struct A<std::vector<T, Alloc> > {...}; 
+7
source

Specialization is as follows:

 // (2) template <class T, class U> class A<std::vector<T, U> > { ... }; 

Please note that work is not guaranteed (and there is no other way that is guaranteed to work), because the number of std::vector template parameters may vary in different implementations. In C ++ 0x, this should be allowed using parameter packages.

+6
source

Source: https://habr.com/ru/post/1335977/


All Articles