23 General utilities library [utilities]

23.15 Metaprogramming and type traits [meta]

23.15.4 Unary type traits [meta.unary]

This subclause contains templates that may be used to query the properties of a type at compile time.

Each of these templates shall be a UnaryTypeTrait with a base characteristic of true_­type if the corresponding condition is true, otherwise false_­type.

23.15.4.1 Primary type categories [meta.unary.cat]

The primary type categories correspond to the descriptions given in section [basic.types] of the C++ standard.

For any given type T, the result of applying one of these templates to T and to cv T shall yield the same result.

[Note: For any given type T, exactly one of the primary type categories has a value member that evaluates to true. end note]

Table 40 — Primary type category predicates
TemplateConditionComments
template <class T>
struct is_­void;
T is void
template <class T>
struct is_­null_­pointer;
T is nullptr_­t ([basic.fundamental])
template <class T>
struct is_­integral;
T is an integral type
template <class T>
struct is_­floating_­point;
T is a floating-point type
template <class T>
struct is_­array;
T is an array type ([basic.compound]) of known or unknown extent Class template array is not an array type.
template <class T>
struct is_­pointer;
T is a pointer type Includes pointers to functions but not pointers to non-static members.
template <class T>
struct is_­lvalue_­reference;
T is an lvalue reference type
template <class T>
struct is_­rvalue_­reference;
T is an rvalue reference type
template <class T>
struct is_­member_­object_­pointer;
T is a pointer to non-static data member
template <class T>
struct is_­member_­function_­pointer;
T is a pointer to non-static member function
template <class T>
struct is_­enum;
T is an enumeration type ([basic.compound])
template <class T>
struct is_­union;
T is a union type ([basic.compound])
template <class T>
struct is_­class;
T is a non-union class type ([basic.compound])
template <class T>
struct is_­function;
T is a function type ([basic.compound])

23.15.4.2 Composite type traits [meta.unary.comp]

These templates provide convenient compositions of the primary type categories, corresponding to the descriptions given in section [basic.types].

For any given type T, the result of applying one of these templates to T and to cv T shall yield the same result.

Table 41 — Composite type category predicates
TemplateConditionComments
template <class T>
struct is_­reference;
T is an lvalue reference or an rvalue reference
template <class T>
struct is_­arithmetic;
T is an arithmetic type
template <class T>
struct is_­fundamental;
T is a fundamental type
template <class T>
struct is_­object;
T is an object type
template <class T>
struct is_­scalar;
T is a scalar type
template <class T>
struct is_­compound;
T is a compound type
template <class T>
struct is_­member_­pointer;
T is a pointer to non-static data member or non-static member function

23.15.4.3 Type properties [meta.unary.prop]

These templates provide access to some of the more important properties of types.

It is unspecified whether the library defines any full or partial specializations of any of these templates.

For all of the class templates X declared in this subclause, instantiating that template with a template-argument that is a class template specialization may result in the implicit instantiation of the template argument if and only if the semantics of X require that the argument must be a complete type.

For the purpose of defining the templates in this subclause, a function call expression declval<T>() for any type T is considered to be a trivial ([basic.types], [special]) function call that is not an odr-use of declval in the context of the corresponding definition notwithstanding the restrictions of [declval].

Table 42 — Type property predicates
TemplateConditionPreconditions
template <class T>
struct is_­const;
T is const-qualified
template <class T>
struct is_­volatile;
T is volatile-qualified
template <class T>
struct is_­trivial;
T is a trivial type remove_­all_­extents_­t<T> shall be a complete type or cv void.
template <class T>
struct is_­trivially_­copyable;
T is a trivially copyable type remove_­all_­extents_­t<T> shall be a complete type or cv void.
template <class T>
struct is_­standard_­layout;
T is a standard-layout type remove_­all_­extents_­t<T> shall be a complete type or cv void.
template <class T>
struct is_­pod;
T is a POD type remove_­all_­extents_­t<T> shall be a complete type or cv void.
template <class T>
struct is_­empty;
T is a class type, but not a union type, with no non-static data members other than bit-fields of length 0, no virtual member functions, no virtual base classes, and no base class B for which is_­empty_­v<B> is false. If T is a non-union class type, T shall be a complete type.
template <class T>
struct is_­polymorphic;
T is a polymorphic class If T is a non-union class type, T shall be a complete type.
template <class T>
struct is_­abstract;
T is an abstract class If T is a non-union class type, T shall be a complete type.
template <class T>
struct is_­final;
T is a class type marked with the class-virt-specifier final (Clause [class]). [Note: A union is a class type that can be marked with final. end note] If T is a class type, T shall be a complete type.
template <class T>
struct is_­aggregate;
T is an aggregate type ([dcl.init.aggr]) remove_­all_­extents_­t<T> shall be a complete type or cv void.
template <class T>
struct is_­signed;
If is_­arithmetic_­v<T> is true, the same result as T(-1) < T(0); otherwise, false
template <class T>
struct is_­unsigned;
If is_­arithmetic_­v<T> is true, the same result as T(0) < T(-1); otherwise, false
template <class T, class... Args>
struct is_­constructible;
For a function type T or for a cv void type T, is_­constructible_­v<T, Args...> is false, otherwise see below T and all types in the parameter pack Args shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­default_­constructible;
is_­constructible_­v<T> is true. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­copy_­constructible;
For a referenceable type T, the same result as is_­constructible_­v<T, const T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­move_­constructible;
For a referenceable type T, the same result as is_­constructible_­v<T, T&&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T, class U>
struct is_­assignable;
The expression declval<T>() = declval<U>() is well-formed when treated as an unevaluated operand. Access checking is performed as if in a context unrelated to T and U. Only the validity of the immediate context of the assignment expression is considered. [Note: The compilation of the expression can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the “immediate context” and can result in the program being ill-formed. end note] T and U shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­copy_­assignable;
For a referenceable type T, the same result as is_­assignable_­v<T&, const T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­move_­assignable;
For a referenceable type T, the same result as is_­assignable_­v<T&, T&&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T, class U>
struct is_­swappable_­with;
The expressions swap(declval<T>(), declval<U>()) and swap(declval<U>(), declval<T>()) are each well-formed when treated as an unevaluated operand in an overload-resolution context for swappable values ([swappable.requirements]). Access checking is performed as if in a context unrelated to T and U. Only the validity of the immediate context of the swap expressions is considered. [Note: The compilation of the expressions can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the “immediate context” and can result in the program being ill-formed. end note] T and U shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­swappable;
For a referenceable type T, the same result as is_­swappable_­with_­v<T&, T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­destructible;
Either T is a reference type, or T is a complete object type for which the expression declval<U&>().~U() is well-formed when treated as an unevaluated operand, where U is remove_­all_­extents<T>. T shall be a complete type, cv void, or an array of unknown bound.
template <class T, class... Args>
struct
is_­trivially_­constructible;
is_­constructible_­v<T,
Args...> is true and the variable definition for is_­constructible, as defined below, is known to call no operation that is not trivial ([basic.types], [special]).
T and all types in the parameter pack Args shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­trivially_­default_­constructible;
is_­trivially_­constructible_­v<T> is true. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­trivially_­copy_­constructible;
For a referenceable type T, the same result as is_­trivially_­constructible_­v<T, const T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­trivially_­move_­constructible;
For a referenceable type T, the same result as is_­trivially_­constructible_­v<T, T&&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T, class U>
struct is_­trivially_­assignable;
is_­assignable_­v<T, U> is true and the assignment, as defined by is_­assignable, is known to call no operation that is not trivial ([basic.types], [special]). T and U shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­trivially_­copy_­assignable;
For a referenceable type T, the same result as is_­trivially_­assignable_­v<T&, const T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­trivially_­move_­assignable;
For a referenceable type T, the same result as is_­trivially_­assignable_­v<T&, T&&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­trivially_­destructible;
is_­destructible_­v<T> is true and the indicated destructor is known to be trivial. T shall be a complete type, cv void, or an array of unknown bound.
template <class T, class... Args>
struct is_­nothrow_­constructible;
is_­constructible_­v<T, Args...> is true and the variable definition for is_­constructible, as defined below, is known not to throw any exceptions ([expr.unary.noexcept]). T and all types in the parameter pack Args shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­nothrow_­default_­constructible;
is_­nothrow_­constructible_­v<T> is true. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­nothrow_­copy_­constructible;
For a referenceable type T, the same result as is_­nothrow_­constructible_­v<T, const T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­nothrow_­move_­constructible;
For a referenceable type T, the same result as is_­nothrow_­constructible_­v<T, T&&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T, class U>
struct is_­nothrow_­assignable;
is_­assignable_­v<T, U> is true and the assignment is known not to throw any exceptions ([expr.unary.noexcept]). T and U shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­nothrow_­copy_­assignable;
For a referenceable type T, the same result as is_­nothrow_­assignable_­v<T&, const T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­nothrow_­move_­assignable;
For a referenceable type T, the same result as is_­nothrow_­assignable_­v<T&, T&&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T, class U>
struct is_­nothrow_­swappable_­with;
is_­swappable_­with_­v<T, U> is true and each swap expression of the definition of is_­swappable_­with<T, U> is known not to throw any exceptions ([expr.unary.noexcept]). T and U shall be complete types, cv void, or arrays of unknown bound.
template <class T>
struct is_­nothrow_­swappable;
For a referenceable type T, the same result as is_­nothrow_­swappable_­with_­v<T&, T&>, otherwise false. T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct is_­nothrow_­destructible;
is_­destructible_­v<T> is true and the indicated destructor is known not to throw any exceptions ([expr.unary.noexcept]). T shall be a complete type, cv void, or an array of unknown bound.
template <class T>
struct has_­virtual_­destructor;
T has a virtual destructor If T is a non-union class type, T shall be a complete type.
template <class T>
struct has_­unique_­object_­representations;
For an array type T, the same result as has_­unique_­object_­representations_­v<remove_­all_­extents_­t<T>>, otherwise see below. T shall be a complete type, cv void, or an array of unknown bound.

[Example:

is_const_v<const volatile int>     // true
is_const_v<const int*>             // false
is_const_v<const int&>             // false
is_const_v<int[3]>                 // false
is_const_v<const int[3]>           // true

end example]

[Example:

remove_const_t<const volatile int>  // volatile int
remove_const_t<const int* const>    // const int*
remove_const_t<const int&>          // const int&
remove_const_t<const int[3]>        // int[3]

end example]

[Example:

// Given:
struct P final { };
union U1 { };
union U2 final { };

// the following assertions hold:
static_assert(!is_final_v<int>);
static_assert(is_final_v<P>);
static_assert(!is_final_v<U1>);
static_assert(is_final_v<U2>);

end example]

The predicate condition for a template specialization is_­constructible<T, Args...> shall be satisfied if and only if the following variable definition would be well-formed for some invented variable t:

T t(declval<Args>()...);

[Note: These tokens are never interpreted as a function declaration. end note] Access checking is performed as if in a context unrelated to T and any of the Args. Only the validity of the immediate context of the variable initialization is considered. [Note: The evaluation of the initialization can result in side effects such as the instantiation of class template specializations and function template specializations, the generation of implicitly-defined functions, and so on. Such side effects are not in the “immediate context” and can result in the program being ill-formed. end note]

The predicate condition for a template specialization has_­unique_­object_­representations<T> shall be satisfied if and only if:

  • T is trivially copyable, and

  • any two objects of type T with the same value have the same object representation, where two objects of array or non-union class type are considered to have the same value if their respective sequences of direct subobjects have the same values, and two objects of union type are considered to have the same value if they have the same active member and the corresponding members have the same value.

The set of scalar types for which this condition holds is implementation-defined. [Note: If a type has padding bits, the condition does not hold; otherwise, the condition holds true for unsigned integral types. end note]