Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement is_stl_vector

I want to specialize a template for STL's vector template arguments. Something like this:

// (1)
template <typename T>
class A
{
    ...
};

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

I don't care what is the type of the vector element. 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've been trying to define something similar to boost's type traits. Something like

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

I cannot use template template (I think so) because it should compile for non-template types too. Is it possible at all?

like image 987
FireAphis Avatar asked Dec 12 '22 16:12

FireAphis


1 Answers

You can simply specialize like this:

// (2)
template <typename T, typename Alloc>
struct A<std::vector<T, Alloc> >
{...};
like image 195
Grizzly Avatar answered Dec 15 '22 04:12

Grizzly