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
)?
C++ Type Traits Standard type traitsThe type_traits header contains a set of template classes and helpers to transform and check properties of types at compile-time. These traits are typically used in templates to check for user errors, support generic programming, and allow for optimizations.
The std::is_same template of C++ STL is used to check whether the type A is same type as of B or not. It return the boolean value true if both are same, otherwise return false. Parameters: This std::is_same template accepts the following parameters: A: It represent the first type.
The type-traits library also contains a set of classes that perform a specific transformation on a type; for example, they can remove a top-level const or volatile qualifier from a type. Each class that performs a transformation defines a single typedef-member type that is the result of the transformation.
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 {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With