Metaprogramming library (since C++11)
C++ provides metaprogramming facilities, such as type traits, compile-time rational arithmetic, and compile-time integer sequences.
Contents |
[edit] Definitions
The following types are collectively called referenceable types:
- object types
- function types without cv and ref
- reference types
For any referenceable type T, a reference to it can be created[1].
- ↑ For reference types, this can be done via reference collapsing.
[edit] Type traits
Type traits define compile-time template-based interfaces to query the properties of types.
Attempting to specialize a template defined in the <type_traits> header and listed in this page results in undefined behavior, except that std::common_type and std::basic_common_reference(since C++20) may be specialized as required in description.
A template defined in the <type_traits> header may be instantiated with an incomplete type unless otherwise specified, notwithstanding the general prohibition against instantiating standard library templates with incomplete types.
[edit] Base classes
Most of non-transforming type traits need to be publicly and unambiguously derived from std::integral_constant in order to satisfy the requirements of UnaryTypeTrait or BinaryTypeTrait.
| Defined in header
<type_traits> | |
| (C++11)(C++17) |
compile-time constant of specified type with specified value (class template) |
Two specializations of std::integral_constant for the type bool are provided:
| Defined in header
<type_traits> | |
| Type | Definition |
true_type
|
std::integral_constant<bool, true> |
false_type
|
std::integral_constant<bool, false> |
[edit] Unary type traits
Unary type traits can be used to query the boolean properties of a type at compile time.
All these type traits satisfy UnaryTypeTrait, the base characteristic of each type trait is either std::true_type or std::false_type, depending on whether the corresponding condition is met.
Primary type categories | |
| Defined in header
<type_traits> | |
| (C++11) |
checks if a type is void (class template) |
| (C++11)(DR*) |
checks if a type is std::nullptr_t (class template) |
| (C++11) |
checks if a type is an integral type (class template) |
| (C++11) |
checks if a type is a floating-point type (class template) |
| (C++11) |
checks if a type is an array type (class template) |
| (C++11) |
checks if a type is an enumeration type (class template) |
| (C++11) |
checks if a type is a union type (class template) |
| (C++11) |
checks if a type is a non-union class type (class template) |
| (C++11) |
checks if a type is a function type (class template) |
| (C++11) |
checks if a type is a pointer type (class template) |
| (C++11) |
checks if a type is an lvalue reference (class template) |
| (C++11) |
checks if a type is an rvalue reference (class template) |
| (C++11) |
checks if a type is a non-static member object pointer (class template) |
| (C++11) |
checks if a type is a non-static member function pointer (class template) |
Composite type categories | |
| Defined in header
<type_traits> | |
| (C++11) |
checks if a type is a fundamental type (class template) |
| (C++11) |
checks if a type is an arithmetic type (class template) |
| (C++11) |
checks if a type is a scalar type (class template) |
| (C++11) |
checks if a type is an object type (class template) |
| (C++11) |
checks if a type is a compound type (class template) |
| (C++11) |
checks if a type is either an lvalue reference or rvalue reference (class template) |
| (C++11) |
checks if a type is a pointer to a non-static member function or object (class template) |
Type properties | |
| Defined in header
<type_traits> | |
| (C++11) |
checks if a type is const-qualified (class template) |
| (C++11) |
checks if a type is volatile-qualified (class template) |
| (C++11)(deprecated in C++26) |
checks if a type is trivial (class template) |
| (C++11) |
checks if a type is trivially copyable (class template) |
| (C++11) |
checks if a type is a standard-layout type (class template) |
| (C++11)(deprecated in C++20) |
checks if a type is a plain-old data (POD) type (class template) |
| (C++11)(deprecated in C++17)(removed in C++20) |
checks if a type is a literal type (class template) |
| checks if every bit in the type's object representation contributes to its value (class template) | |
| (C++11) |
checks if a type is a class (but not union) type and has no non-static data members (class template) |
| (C++11) |
checks if a type is a polymorphic class type (class template) |
| (C++11) |
checks if a type is an abstract class type (class template) |
| (C++14) |
checks if a type is a final class type (class template) |
| (C++17) |
checks if a type is an aggregate type (class template) |
| (C++23) |
checks if a type is an implicit-lifetime type (class template) |
| (C++11) |
checks if a type is a signed arithmetic type (class template) |
| (C++11) |
checks if a type is an unsigned arithmetic type (class template) |
| (C++20) |
checks if a type is an array type of known bound (class template) |
| (C++20) |
checks if a type is an array type of unknown bound (class template) |
| (C++23) |
checks if a type is a scoped enumeration type (class template) |
Supported operations | |
| Defined in header |