Partager via


<type_traits>

 

The latest version of this topic can be found at <type_traits>.

Defines templates that provide compile-time constants that give information about the properties of their type arguments or produce transformed types.

Syntax

#include <type_traits>  

Remarks

The classes and templates in <type_traits> are used to support type inference, classification, and transformation at compile time, to detect type-related errors, and to help you optimize your generic code. These classes and templates include unary type traits that describe a property of a type, binary type traits that describe a relationship between types, and transformation traits that modify a property of a type.

To support type traits, a helper class, integral_constant, is defined. It has template specializations true_type and false_type that form the base classes for type predicates. A type predicate is a template that takes one or more type arguments. When a type predicate holds true, it is publicly derived, directly or indirectly, from true_type. When a type predicate holds false, it is publicly derived, directly or indirectly, from false_type.

A type modifier or transformation trait is a template that takes one or more template arguments and has one member, type, which is a synonym for the modified type.

Alias Templates

To simplify type traits expressions, alias templates for typename some_trait<T>::type are provided, where " some_trait" is the template class name. For example, add_const has an alias template for its type, add_const_t, defined as:

template <class T>
using add_const_t = typename add_const<T>::type;
add_const_t aligned_storage_t make_signed_t remove_pointer_t
add_cv_t aligned_union_t make_unsigned_t remove_reference_t
add_lvalue_reference_t common_type_t remove_all_extents_t remove_volatile_t
add_pointer_t conditional_t remove_const_t result_of_t
add_rvalue_reference_t decay_t remove_cv_t underlying_type_t
add_volatile_t enable_if_t remove_extent_t

Classes

Helper class and typedefs

integral_constant Makes an integral constant from a type and a value.
true_type Holds integral constant with true value.
false_type Holds integral constant with false value.

Primary type categories

is_void Tests whether the type is void.
is_null_pointer Tests whether the type is std::nullptr_t.
is_integral Tests whether the type is integral.
is_floating_point Tests whether the type is floating-point.
is_array Tests whether the type is an array.
is_pointer Tests whether the type is a pointer.
is_lvalue_reference Tests if type is an lvalue reference.
is_rvalue_reference Tests if type is an rvalue reference.
is_member_object_pointer Tests whether the type is a pointer to a member object.
is_member_function_pointer Tests whether the type is a pointer to a member function.
is_enum Tests whether the type is an enumeration.
is_union Tests whether the type is a union.
is_class Tests whether the type is a class.
is_function Tests whether the type is a function type.

Composite type categories

is_reference Tests whether the type is a reference.
is_arithmetic Tests whether the type is arithmetic.
is_fundamental Tests whether the type is void or arithmetic.
is_object Tests whether the type is an object type.
is_scalar Tests whether the type is scalar.
is_compound Tests whether the type is not scalar.
is_member_pointer Tests whether the type is a pointer to a member.

Type properties

is_const Tests whether the type is const.
is_volatile Tests whether the type is volatile.
is_trivial Tests whether the type is trivial.
is_trivially_copyable Tests whether the type is trivially copyable.
is_standard_layout Tests if type is a standard layout type.
is_pod Tests whether the type is a POD.
is_literal_type Tests whether the type can be a constexpr variable or used in a constexpr function.
is_empty Tests whether the type is an empty class.
is_polymorphic Tests whether the type is a polymorphic class.
is_abstract Tests whether the type is an abstract class.
is_final Tests whether the type is a class type marked final.
is_signed Tests whether the type is a signed integer.
is_unsigned Tests whether the type is an unsigned integer.
is_constructible Tests whether the type is constructible using the specified argument types.
is_default_constructible Tests whether the type has a default constructor.
is_copy_constructible Tests whether the type has a copy constructor.
is_move_constructible Tests whether the type has a move constructor.
is_assignable Tests whether the first type can be assigned a value of the second type.
is_copy_assignable Tests whether a type can be assigned a const reference value of the type.
is_move_assignable Tests whether a type can be assigned an rvalue reference of the type.
is_destructible Tests whether the type is destructible.
is_trivially_constructible Tests whether the type uses no non-trivial operations when constructed using the specified types.
is_trivially_default_constructible Tests whether the type uses no non-trivial operations when default constructed.
is_trivially_copy_constructible Tests whether the type uses no non-trivial operations when copy constructed.
is_trivially_move_constructible Tests whether the type uses no non-trivial operations when move constructed.
is_trivially_assignable Tests whether the types are assignable and the assignment uses no non-trivial operations.
is_trivially_copy_assignable Tests whether the type is copy assignable and the assignment uses no non-trivial operations.
is_trivially_move_assignable Tests whether the type is move assignable and the assignment uses no non-trivial operations.
is_trivially_destructible Tests whether the type is destructible and the destructor uses no non-trivial operations.
is_nothrow_constructible Tests whether the type is constructible and is known not to throw when constructed using the specified types.
is_nothrow_default_constructible Tests whether the type is default constructible and is known not to throw when default constructed.
is_nothrow_copy_constructible Tests whether the type is copy constructible and the copy constructor is known not to throw.
is_nothrow_move_constructible Tests whether the type is move constructible and the move constructor is known not to throw.
is_nothrow_assignable Tests whether the type is assignable using the specified type and the assignment is known not to throw.
is_nothrow_copy_assignable Tests whether the type is copy assignable and the assignment is known not to throw.
is_nothrow_move_assignable Tests whether the type is move assignable and the assignment is known not to throw.
is_nothrow_destructible Tests whether the type is destructible and the destructor is known not to throw.
has_virtual_destructor Tests whether the type has a virtual destructor.

Type property queries

alignment_of Gets the alignment of a type.
rank Gets the number of array dimensions.
extent Gets the number of elements in the specified array dimension.

Type relations

is_same Tests whether two types are the same.
is_base_of Tests whether one type is a base of another.
is_convertible Tests whether one type is convertible to another.

Const-volatile modifications

add_const Produces a const type from type.
add_volatile Produces a volatile type from type.
add_cv Produces a const``volatile type from type.
remove_const Produces a non-const type from type.
remove_volatile Produces a non-volatile type from type.
remove_cv Produces a non-const non-volatile type from type.

Reference modifications

add_lvalue_reference Produces a reference to type from type.
add_rvalue_reference Produces an rvalue reference to type from type
remove_reference Produces a non-reference type from type.

Sign modifications

make_signed Produces the type if signed, or the smallest signed type greater than or equal in size to type.
make_unsigned Produces the type if unsigned, or the smallest unsigned type greater than or equal in size to type.

Array modifications

remove_all_extents Produces a non-array type from an array type.
remove_extent Produces the element type from an array type.

Pointer modifications

add_pointer Produces a pointer to type from type.
remove_pointer Produces a type from a pointer to type.

Other transformations

aligned_storage Allocates uninitialized memory for an aligned type.
aligned_union Allocates uninitialized memory for an aligned union with a non-trivial constructor or destructor.
common_type Produces the common type of all the types of the parameter pack.
conditional If the condition is true, produces the first specified type, otherwise the second specified type.
decay Produces the type as passed by value. Makes non-reference, non-const, or non-volatile type, or makes a pointer to type.
enable_if If the condition is true, produces the specified type, otherwise no type.
result_of Determines the return type of the callable type that takes the specified argument types.
underlying_type Produces the underlying integral type for an enumeration type.

See Also

<functional>