Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 is_same type trait for templates

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)?

like image 434
user2052436 Avatar asked Jun 03 '13 20:06

user2052436


People also ask

What are C++ type traits?

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.

Is C++ same template?

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.

How do type traits work C++?

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.


1 Answers

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 {}; 
like image 155
jrok Avatar answered Sep 21 '22 02:09

jrok