C++ Reference ยท Types and Language Concepts
Type traits and template metaprogramming
Reference for C++ type traits and template metaprogramming: querying and transforming types at compile time, _v and _t helpers, if constexpr and requires.
A type trait is a class template from <type_traits> that answers a question about a type at compile time (std::is_integral<T>) or builds a new type from an old one (std::remove_const<T>). Template code uses traits to choose an implementation, to reject unsuitable types with a clear message, and to compute result types.
Traits are the core vocabulary of template metaprogramming: programming that runs inside the compiler. Modern C++ replaces most of the old tricks (enable_if, SFINAE) with if constexpr, static_assert and concepts, but traits remain the building blocks those features are written with.
#include <type_traits> static_assert(std::is_integral_v<int>); using Plain = std::remove_cvref_t<const int&>; // int
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.
How traits work
Every trait is a class template. A UnaryTypeTrait such as std::is_pointer<T> derives from std::true_type or std::false_type, so it has a static value member. A transformation trait such as std::remove_const<T> has a nested type.
C++17 added _v variable templates for the first kind (std::is_pointer_v<T> means std::is_pointer<T>::value). C++14 added _t aliases for the second kind (std::remove_const_t<T> means typename std::remove_const<T>::type). Always prefer the short forms.
Main families
| Family | Examples |
|---|---|
| Primary categories | is_integral, is_floating_point, is_pointer, is_array, is_class, is_enum, is_function |
| Properties | is_const, is_signed, is_trivially_copyable, is_abstract, is_final, is_empty |
| Relationships | is_same, is_base_of, is_convertible, is_invocable |
| Constructibility | is_constructible, is_nothrow_move_constructible, is_default_constructible |
| Modifications | remove_const, remove_reference, remove_cvref (C++20), add_pointer, decay, make_signed |
| Selection | conditional, common_type, invoke_result, underlying_type |
Choosing code with if constexpr
if constexpr discards the branch that does not apply, so the discarded branch may contain code that would not compile for that type. This is the tool to use instead of overloads with std::enable_if.
Detecting members
The has_size trait in the second example is the detection idiom: a partial specialization is viable only when decltype(...) is a valid expression (substitution failure is not an error, SFINAE). In C++20 a concept does the same job in one line: template <typename T> concept HasSize = requires(T t) { t.size(); };.
Usage notes
- Traits are about types, not values. Use
std::decay_torstd::remove_cvref_tfirst when the template parameter may be a reference or haveconst. is_sameis exact.intandconst intdiffer, and so docharandsigned char.- Undefined behavior: instantiating most traits with an incomplete type is undefined behavior. Specializing a standard trait for your own type is forbidden, except for the few that explicitly allow it (
std::common_type,std::hash-style customization points). is_trivially_copyableis the gate formemcpy. Only memcpy objects whose type satisfies it.- Traits cost nothing at run time. They only affect compilation; heavy use can slow the compiler.
- Prefer concepts for constraints. Write
template <std::integral T>instead ofenable_if. Concepts give better error messages and are easier to read. - Added in:
<type_traits>in C++11;_vand_thelpers in C++14/17;remove_cvref,is_bounded_arrayandtype_identityin C++20;is_scoped_enumin C++23.
Related
- template
- concept and requires
- static_assert
- CRTP
- Tutorial: Type traits, Concepts