型があるtemplate class型であるかチェックするメタ関数


例 vectorかチェックする

main.cpp

//vectorかチェック
template<class Type>
struct is_vector : std::false_type
{};
template<class ValueType,class Alloc>
struct is_vector<std::vector<ValueType,Alloc>> : std::true_type
{};

int main()
{
    is_vector<std::vector<int>>::value; //true

    return 0;
}

上のように特殊化をしてやれば簡単に実装できますが、is_list,is_mapなどいちいち用意していては大変なので
テンプレートテンプレートパラメータをうけとり、まとめて特殊化してあげればよい

is_template

main.cpp

//TypeがTemplate型であるか
template<template <class...>class Template,class Type>
struct is_template : std::false_type
{};
template<template <class...>class Template, class... Args>
struct is_template<Template, Template<Args...>> : std::true_type
{};

int main()
{
    is_template<std::vector,std::vector<int>>::value;   //true
    is_template<std::list, std::list<int>>::value;      //true
    is_template<std::map, std::map<int,double>>::value; //true

    return 0;
}


またエイリアステンプレートでラップをかけてあげてば、それぞれのメタ関数として使える

main.cpp
template<class T>
using is_vector = is_template<std::vector, T>;

template<class T>
using is_list = is_template<std::list, T>;

template<class T>
using is_map = is_template<std::map, T>;