Standard type traits

suggest change

C++11

The 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.


Most type traits are used to check if a type fulfils some criteria. These have the following form:

template <class T> struct is_foo;

If the template class is instantiated with a type which fulfils some criteria foo, then is_foo<T> inherits from std::integral_constant<bool,true> (a.k.a. std::true_type), otherwise it inherits from std::integral_constant<bool,false> (a.k.a. std::false_type). This gives the trait the following members:

Constants

static constexpr bool value

true if T fulfils the criteria foo, false otherwise

Functions

operator bool

Returns value

C++14

bool operator()

Returns value

Types

Name Definition
value_type bool
type std::integral_constant<bool,value>

The trait can then be used in constructs such as static_assert or std::enable_if. An example with std::is_pointer:

template <typename T>
void i_require_a_pointer (T t) {
    static_assert(std::is_pointer<T>::value, "T must be a pointer type");
}

//Overload for when T is not a pointer type
template <typename T>
typename std::enable_if<!std::is_pointer<T>::value>::type
does_something_special_with_pointer (T t) {
    //Do something boring
}

//Overload for when T is a pointer type
template <typename T>
typename std::enable_if<std::is_pointer<T>::value>::type 
does_something_special_with_pointer (T t) {
    //Do something special
}

There are also various traits which transform types, such as std::add_pointer and std::underlying_type. These traits generally expose a single type member type which contains the transformed type. For example, std::add_pointer<int>::type is int*.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Type traits:
* Standard type traits

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
98 Type traits
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype