25
votes

I've been on this problem all morning with no result whatsoever. Basically, I need a simple metaprogramming thing that allows me to branch to different specializations if the parameter passed is a kind of std::vector or not.

Some kind of is_base_of for templates.

Does such a thing exist ?

3
With "specialization" do you mean inheritance? Or a type alias (e.g. typedef)? Or a specialized implementation for a certain type (like std::vector<bool> is)?Some programmer dude
Your question is vague: if you want to determine if a type is a template specialization of std::vector for a type, you shouldn't be able to do it (not in a clean way anyway). If you want to determine if a type is inherited from std::vector<XXX>, this is explicitly advised against (std::vector doesn't have a virtual destructor and SHOULD NOT be inherited, only encapsulated). If you want to determine if a class/typedef/template parameter is a std::vector<XYZ, ...>, you should use a templated traits class (see answer from jrok).utnapistim
@utnapistim: It is not hard to check if a type is a specialization of a template in general.David Rodríguez - dribeas
@DavidRodríguez-dribeas - I didn't mean check that a type is a std::vector<XYZ>, but that there is a template<> class vector<XYZ> particularized implementation (similar to how struct is_std_vector<std::vector<T,A>> is a particularized implementation of struct is_std_vector in jirok's answer). Is there a way of doing that?utnapistim
@utnapistim: I guess this is just a misunderstanding due to the overuse of the term 'specialization' in the standard meaning both when there is a separate definition for a set of template arguments and also the type/function generated after substitution of the template arguments in the base template. AFAIK you are right in that you cannot detect whether there is a different definition for a particular set of template arguments.David Rodríguez - dribeas

3 Answers

28
votes

In C++11 you can also do it in a more generic way:

#include <type_traits>
#include <iostream>
#include <vector>
#include <list>

template<typename Test, template<typename...> class Ref>
struct is_specialization : std::false_type {};

template<template<typename...> class Ref, typename... Args>
struct is_specialization<Ref<Args...>, Ref>: std::true_type {};


int main()
{
    typedef std::vector<int> vec;
    typedef int not_vec;
    std::cout << is_specialization<vec, std::vector>::value << is_specialization<not_vec, std::vector>::value;

    typedef std::list<int> lst;
    typedef int not_lst;
    std::cout << is_specialization<lst, std::list>::value << is_specialization<not_lst, std::list>::value;
}
23
votes

If you need a trait class it's pretty simple, you only need a general template and a specialization over any std::vector:

#include <type_traits>
#include <iostream>
#include <vector>

template<typename>
struct is_std_vector : std::false_type {};

template<typename T, typename A>
struct is_std_vector<std::vector<T,A>> : std::true_type {};

int main()
{
    typedef std::vector<int> vec;
    typedef int not_vec;
    std::cout << is_std_vector<vec>::value << is_std_vector<not_vec>::value;
}
5
votes

No, but you can overload with a template function which only accepts std::vector<T>. The compiler will choose the most specialized template in such cases.