C++ Reference ยท Memory and Utilities
The C++ <type_traits> header
Reference for the C++ <type_traits> header: query and transform types at compile time with is_integral, remove_reference, decay, conditional and more.
The <type_traits> header contains class templates that inspect and transform types at compile time. A trait is a template such as std::is_integral<T> or std::remove_const<T>. Query traits expose a value member (true or false); transformation traits expose a type member holding the new type. Since C++17 every query has a variable-template shortcut ending in _v (std::is_integral_v<T>) and every transformation an alias ending in _t (std::remove_const_t<T>).
Traits are the building blocks of generic code: they let a template choose between implementations, reject unsuitable types with a clear message, or compute the right return type. In C++20 many uses are replaced by concepts, but traits remain the vocabulary concepts are written in.
static_assert(std::is_integral_v<int>); using Plain = std::remove_cvref_t<const std::string&>; // std::string
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class T, T v> struct integral_constant { static constexpr T value = v; }; using true_type = integral_constant<bool, true>; using false_type = integral_constant<bool, false>; // Query: std::is_xxx<T>::value or std::is_xxx_v<T> // Transform: typename std::xxx<T>::type or std::xxx_t<T>
| Group | Examples |
|---|---|
| Primary categories | is_void, is_integral, is_floating_point, is_array, is_pointer, is_reference, is_enum, is_class, is_function |
| Composite categories | is_arithmetic, is_fundamental, is_scalar, is_object, is_compound |
| Properties | is_const, is_volatile, is_signed, is_unsigned, is_trivially_copyable, is_standard_layout, is_empty, is_abstract, is_final, is_polymorphic |
| Operations | is_constructible, is_default_constructible, is_copy_constructible, is_move_constructible, is_assignable, is_destructible, is_nothrow_*, is_invocable |
| Relationships | is_same, is_base_of, is_convertible, is_layout_compatible (see is_same and friends) |
| Modifications | remove_const, remove_reference, remove_cvref (C++20), add_pointer, add_lvalue_reference, make_signed, make_unsigned, remove_extent |
| Other | decay, conditional, enable_if (see enable_if), common_type, underlying_type, invoke_result, type_identity (C++20), void_t |
Returns: value is a constexpr bool for queries; type is the resulting type for transformations.
Errors / undefined behavior: adding specializations of standard traits (other than a few documented ones such as std::common_type) is undefined behavior. Asking about an incomplete type gives undefined behavior for most traits. Some traits need complete types and fail to compile otherwise.
Usage notes
- Use the
_vand_tforms (C++17). They avoid::value,typenameand::typenoise. if constexprplus traits replaces most tag dispatch andenable_ifcode: only the matching branch is instantiated.remove_cvref_tis the usual "clean type" for a forwarding reference parameterT&&.decay_tadditionally turns arrays and functions into pointers.is_integralincludesboolandchartypes.int,short,long long,char,char8_t,bool,wchar_tand their cv-qualified forms are integral;std::byteis not (it is an enum).is_trivially_copyabletells you whethermemcpyis a legal way to copy an object. Standard containers and strings are not.- Query results are compile-time constants and can feed
static_assert, template arguments andconstexprfunctions. - Compiler-supported traits such as
is_enum,is_classandis_trivially_copyablecannot be written in portable C++, so the library relies on built-ins. - Related headers:
<concepts>(C++20) builds on these;std::numeric_limitsanswers value-level questions about arithmetic types.