Breyta

Deila með


<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 <class T>
using add_const_t = typename add_const<T>::type;

These are the provided aliases for the type members:

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\

common_type_t
conditional_t
decay_t
enable_if_t
invoke_result_t
make_signed_t
make_unsigned_t
remove_all_extents_t\

remove_const_t
remove_cv_t
remove_extent_t
remove_pointer_t
remove_reference_t
remove_volatile_t
result_of_t
underlying_type_t\

Classes

Helper class and typedefs

Name Description
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

Name Description
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

Name Description
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

Name Description
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_aggregate
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_swappable
is_swappable_with
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_swappable
is_nothrow_swappable_with
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.
has_unique_object_representations
is_invocable Tests whether a callable type can be invoked using the specified argument types.
Added in C++17.
is_invocable_r Tests 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_invocable Tests 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_r Tests 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

Name Description
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

Name Description
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

Name Description
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

Name Description
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

Name Description
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

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

Pointer modifications

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

Other transformations

Name Description
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.
invoke_result Determines the return type of the callable type that takes the specified argument types.
Added in C++17.
result_of Determines the return type of the callable type that takes the specified argument types.
Added in C++14, deprecated in C++17.
underlying_type Produces the underlying integral type for an enumeration type.

Logical operator traits

Name Description
conjunction
disjunction
negation

See also

<functional>