<type_traits>

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

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.

#include <type_traits>  

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_taligned_storage_tmake_signed_tremove_pointer_t
add_cv_taligned_union_tmake_unsigned_tremove_reference_t
add_lvalue_reference_tcommon_type_tremove_all_extents_tremove_volatile_t
add_pointer_tconditional_tremove_const_tresult_of_t
add_rvalue_reference_tdecay_tremove_cv_tunderlying_type_t
add_volatile_tenable_if_tremove_extent_t

Classes

Helper class and typedefs

integral_constantMakes an integral constant from a type and a value.
true_typeHolds integral constant with true value.
false_typeHolds integral constant with false value.

Primary type categories

is_voidTests whether the type is void.
is_null_pointerTests whether the type is std::nullptr_t.
is_integralTests whether the type is integral.
is_floating_pointTests whether the type is floating-point.
is_arrayTests whether the type is an array.
is_pointerTests whether the type is a pointer.
is_lvalue_referenceTests if type is an lvalue reference.
is_rvalue_referenceTests if type is an rvalue reference.
is_member_object_pointerTests whether the type is a pointer to a member object.
is_member_function_pointerTests whether the type is a pointer to a member function.
is_enumTests whether the type is an enumeration.
is_unionTests whether the type is a union.
is_classTests whether the type is a class.
is_functionTests whether the type is a function type.

Composite type categories

is_referenceTests whether the type is a reference.
is_arithmeticTests whether the type is arithmetic.
is_fundamentalTests whether the type is void or arithmetic.
is_objectTests whether the type is an object type.
is_scalarTests whether the type is scalar.
is_compoundTests whether the type is not scalar.
is_member_pointerTests whether the type is a pointer to a member.

Type properties

is_constTests whether the type is const.
is_volatileTests whether the type is volatile.
is_trivialTests whether the type is trivial.
is_trivially_copyableTests whether the type is trivially copyable.
is_standard_layoutTests if type is a standard layout type.
is_podTests whether the type is a POD.
is_literal_typeTests whether the type can be a constexpr variable or used in a constexpr function.
is_emptyTests whether the type is an empty class.
is_polymorphicTests whether the type is a polymorphic class.
is_abstractTests whether the type is an abstract class.
is_finalTests whether the type is a class type marked final.
is_signedTests whether the type is a signed integer.
is_unsignedTests whether the type is an unsigned integer.
is_constructibleTests whether the type is constructible using the specified argument types.
is_default_constructibleTests whether the type has a default constructor.
is_copy_constructibleTests whether the type has a copy constructor.
is_move_constructibleTests whether the type has a move constructor.
is_assignableTests whether the first type can be assigned a value of the second type.
is_copy_assignableTests whether a type can be assigned a const reference value of the type.
is_move_assignableTests whether a type can be assigned an rvalue reference of the type.
is_destructibleTests whether the type is destructible.
is_trivially_constructibleTests whether the type uses no non-trivial operations when constructed using the specified types.
is_trivially_default_constructibleTests whether the type uses no non-trivial operations when default constructed.
is_trivially_copy_constructibleTests whether the type uses no non-trivial operations when copy constructed.
is_trivially_move_constructibleTests whether the type uses no non-trivial operations when move constructed.
is_trivially_assignableTests whether the types are assignable and the assignment uses no non-trivial operations.
is_trivially_copy_assignableTests whether the type is copy assignable and the assignment uses no non-trivial operations.
is_trivially_move_assignableTests whether the type is move assignable and the assignment uses no non-trivial operations.
is_trivially_destructibleTests whether the type is destructible and the destructor uses no non-trivial operations.
is_nothrow_constructibleTests whether the type is constructible and is known not to throw when constructed using the specified types.
is_nothrow_default_constructibleTests whether the type is default constructible and is known not to throw when default constructed.
is_nothrow_copy_constructibleTests whether the type is copy constructible and the copy constructor is known not to throw.
is_nothrow_move_constructibleTests whether the type is move constructible and the move constructor is known not to throw.
is_nothrow_assignableTests whether the type is assignable using the specified type and the assignment is known not to throw.
is_nothrow_copy_assignableTests whether the type is copy assignable and the assignment is known not to throw.
is_nothrow_move_assignableTests whether the type is move assignable and the assignment is known not to throw.
is_nothrow_destructibleTests whether the type is destructible and the destructor is known not to throw.
has_virtual_destructorTests whether the type has a virtual destructor.

Type property queries

alignment_ofGets the alignment of a type.
rankGets the number of array dimensions.
extentGets the number of elements in the specified array dimension.

Type relations

is_sameTests whether two types are the same.
is_base_ofTests whether one type is a base of another.
is_convertibleTests whether one type is convertible to another.

Const-volatile modifications

add_constProduces a const type from type.
add_volatileProduces a volatile type from type.
add_cvProduces a const``volatile type from type.
remove_constProduces a non-const type from type.
remove_volatileProduces a non-volatile type from type.
remove_cvProduces a non-const non-volatile type from type.

Reference modifications

add_lvalue_referenceProduces a reference to type from type.
add_rvalue_referenceProduces an rvalue reference to type from type
remove_referenceProduces a non-reference type from type.

Sign modifications

make_signedProduces the type if signed, or the smallest signed type greater than or equal in size to type.
make_unsignedProduces the type if unsigned, or the smallest unsigned type greater than or equal in size to type.

Array modifications

remove_all_extentsProduces a non-array type from an array type.
remove_extentProduces the element type from an array type.

Pointer modifications

add_pointerProduces a pointer to type from type.
remove_pointerProduces a type from a pointer to type.

Other transformations

aligned_storageAllocates uninitialized memory for an aligned type.
aligned_unionAllocates uninitialized memory for an aligned union with a non-trivial constructor or destructor.
common_typeProduces the common type of all the types of the parameter pack.
conditionalIf the condition is true, produces the first specified type, otherwise the second specified type.
decayProduces the type as passed by value. Makes non-reference, non-const, or non-volatile type, or makes a pointer to type.
enable_ifIf the condition is true, produces the specified type, otherwise no type.
result_ofDetermines the return type of the callable type that takes the specified argument types.
underlying_typeProduces the underlying integral type for an enumeration type.

<functional>

Show: