Search code examples
c++templatesc++11metaprogrammingtype-traits

How to check if a type is a specialization of the std::array class template


Is it possible to check that type T is an std::array of arbitrary type and size?

I can check for a particular array, for instance:

is_same<T, std::array<int,5>>::value

But I'd like to check that T is any instantiation of std::array. Something like below (which, of course, does not compile):

is_same<T, std::array>::value

Is there a way to achieve this (maybe not using is_same)?


Solution

  • You have to write your own, but it's simple:

    template<typename>
    struct is_std_array : std::false_type {};
    
    template<typename T, std::size_t N>
    struct is_std_array<std::array<T,N>> : std::true_type {};