std::vector<int> 型であるかどうかであれば、<type_traits> に定義されている std::is_same を使うことで、調べることができます。
しかし、要素の型を問わない std::vector<*> 型であるかどうかを直接調べる手段は、<type_traits> にも用意されていません。
専用のテンプレート型を用意することで、ある型が std::vector 型であるかどうかを調べることが可能です。is_vector の実装例を示します。
#include <deque>
#include <iostream>
#include <type_traits>
#include <vector>
// type checker of std::vector
template<typename>
struct is_vector : std::false_type {};
template<typename T, typename ALLOCATOR>
struct is_vector<std::vector<T, ALLOCATOR>> : std::true_type {};
int main(void)
{
// vはstd::vectorである
std::vector<int> v;
std::cout << is_vector<decltype(v)>::value; // => "1"
// dはstd::vectorではない
std::deque<int> d;
std::cout << is_vector<decltype(d)>::value; // => "0"
return 0;
}
残念ながら、is_vector は std::vector であるかどうかを調べるためだけのクラスです。std::map であるかどうかを調べるには、別途 is_map のようなクラスを定義する必要があります。
0 件のコメント:
コメントを投稿