Skip to content

Latest commit

 

History

History
245 lines (204 loc) · 16.2 KB

type-traits.md

File metadata and controls

245 lines (204 loc) · 16.2 KB
descriptiontitlems.datef1_keywordshelpviewer_keywordsms.assetid
Learn more about: <type_traits>
<type_traits>
02/21/2019
<type_traits>
typetrait header
type_traits
2260b51f-8160-4c66-a82f-00b534cb60d4

<type_traits>

Defines templates for 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. They are also used to detect type-related errors, and to help you optimize your generic code. Unary type traits describe a property of a type, binary type traits describe a relationship between types, and transformation traits modify a property of a type.

The helper class integral_constant and its template specializations true_type and false_type 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's publicly derived, directly or indirectly, from true_type. When a type predicate holds false, it's 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 class template name. For example, add_const has an alias template for its type, add_const_t, defined as:

template <classT> usingadd_const_t = typename add_const<T>::type;

These are the provided aliases for the type members:

:::row::: :::column::: add_const_t
add_cv_t
add_lvalue_reference_t
add_pointer_t
add_rvalue_reference_t
add_volatile_t
aligned_storage_t
aligned_union_t
:::column-end::: :::column::: common_type_t
conditional_t
decay_t
enable_if_t
invoke_result_t
make_signed_t
make_unsigned_t
remove_all_extents_t
:::column-end::: :::column::: remove_const_t
remove_cv_t
remove_extent_t
remove_pointer_t
remove_reference_t
remove_volatile_t
result_of_t
underlying_type_t
:::column-end::: :::row-end:::

Classes

Helper class and typedefs

NameDescription
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

NameDescription
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

NameDescription
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

NameDescription
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_aggregate
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_swappable
is_swappable_with
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_swappable
is_nothrow_swappable_with
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.
has_unique_object_representations
is_invocableTests whether a callable type can be invoked using the specified argument types.
Added in C++17.
is_invocable_rTests whether a callable type can be invoked using the specified argument types and the result is convertible to the specified type.
Added in C++17.
is_nothrow_invocableTests whether a callable type can be invoked using the specified argument types and is known not to throw exceptions.
Added in C++17.
is_nothrow_invocable_rTests whether a callable type can be invoked using the specified argument types and is known not to throw exceptions, and the result is convertible to the specified type.
Added in C++17.

Type property queries

NameDescription
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

NameDescription
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

NameDescription
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

NameDescription
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

NameDescription
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

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

Pointer modifications

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

Other transformations

NameDescription
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.
invoke_resultDetermines the return type of the callable type that takes the specified argument types.
Added in C++17.
result_ofDetermines the return type of the callable type that takes the specified argument types.
Added in C++14, deprecated in C++17.
underlying_typeProduces the underlying integral type for an enumeration type.

Logical operator traits

NameDescription
conjunction
disjunction
negation

See also

<functional>

close