7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

例 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>;
7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?