skip to content

What does a C++ type trait such as std::is_integral<T> give you, and what are its _v and _t helpers?

level: juniorimportance: should knowfreq 55%

answer

  1. questions about types, at compile time
  2. ::value or ::type
  3. integral_constant underneath
  4. _t in C++14, _v in C++17

basics

~20 s

A type trait is a class template in <type_traits> that answers a compile-time question about a type: a bool in ::value (is_integral) or a transformed type in ::type (remove_const). The _t alias templates (C++14) and _v variable templates (C++17) shorten both.

solid answer

~30 s

`<type_traits>` holds class templates that inspect or transform types at compile time, with no run-time cost. **Value traits** such as `std::is_integral<T>`, `std::is_enum<T>` or `std::is_trivially_copyable<T>` derive from `std::integral_constant<bool, B>`, so the answer is `std::is_integral<long>::value`, and the trait object itself is a `std::true_type` or `std::false_type`. **Transformation traits** such as `std::remove_const<T>` or `std::underlying_type<T>` expose the result as a nested `::type`. Because `typename std::remove_const<T>::type` is noisy, C++14 added alias templates like `std::remove_const_t<T>`, and C++17 added variable templates like `std::is_integral_v<T>`. A serializer uses them in `static_assert`, `if constexpr` or as template arguments to decide, per type, how to write a value.

code

cpp · 25 lines
cpp
#include <iostream>
#include <string>
#include <type_traits>

enum class Kind : unsigned char { ping = 1 };

// Value traits: a bool in ::value, or _v since C++17.
static_assert(std::is_integral<long>::value);
static_assert(std::is_integral_v<long>);
static_assert(std::is_enum_v<Kind> && !std::is_enum_v<int>);
static_assert(std::is_trivially_copyable_v<double>);
static_assert(!std::is_trivially_copyable_v<std::string>);
static_assert(!std::is_same_v<int, const int>);

// Transformation traits: a type in ::type, or _t since C++14.
static_assert(std::is_same_v<std::remove_const_t<const int>, int>);
static_assert(std::is_same_v<std::underlying_type_t<Kind>, unsigned char>);

// The base class behind every value trait.
static_assert(std::is_base_of_v<std::true_type, std::is_integral<long>>);

int main() {
    std::cout << std::boolalpha << std::is_pointer_v<const char*> << ' '
              << std::is_pointer_v<char[4]> << '\n';   // true false
}

go deeper

for a junior

Know where traits live, read is_integral_v and remove_const_t fluently, and remember that the answer is a compile-time constant or type.

for a middle

Explain the integral_constant base, the difference between value and transformation traits, and which standard added the _t and _v helpers.

for a senior

Use traits deliberately in static_assert, if constexpr and tag dispatch, and know which standard traits users may and may not specialize.

for a principal

Decide when a codebase should wrap raw traits in named concepts or project traits so that requirements read as domain rules.

## What a type trait is A **type trait** is a small class template that answers a question about a type, or computes a new type from it, entirely at compile time. The standard collects them in `<type_traits>`. Nothing runs at run time: the answer is a constant or a type that the compiler substitutes into your code. The standard classifies them in three groups: | Kind | Example | Answer lives in | |---|---|---| | unary type trait | `std::is_integral<T>`, `std::is_enum<T>`, `std::is_trivially_copyable<T>` | `::value` (a `bool` or other constant) | | binary type trait | `std::is_same<T, U>`, `std::is_base_of<B, D>`, `std::is_convertible<From, To>` | `::value` | | transformation trait | `std::remove_const<T>`, `std::decay<T>`, `std::underlying_type<T>` | `::type` | ## integral_constant underneath Every unary and binary trait derives, directly or indirectly, from a specialization of `std::integral_constant<T, v>`, called its **base characteristic**. For boolean traits that is `std::integral_constant<bool, true>` (aliased `std::true_type`) or `std::integral_constant<bool, false>` (`std::false_type`). This gives three ways to use one trait: - read the constant: `std::is_integral<long>::value`; - use the object as a tag: `std::is_integral<long>{}` converts to `bool` and can select an overload taking `std::true_type`; - inherit from it when writing your own trait: `struct is_wire_type : std::true_type {};`. `std::bool_constant<B>` (C++17) is the alias for `std::integral_constant<bool, B>`. ## The _v and _t helpers The long forms are verbose, especially in dependent contexts where `typename` is required: ```cpp typename std::remove_const<T>::type // C++11 std::remove_const_t<T> // C++14 alias template std::is_integral<T>::value // C++11 std::is_integral_v<T> // C++17 variable template ``` - The **`_t` alias templates** arrived in **C++14** for every transformation trait. - The **`_v` variable templates** arrived in **C++17** for every value trait. - Neither adds behaviour; `std::is_integral_v<T>` is defined as `std::is_integral<T>::value`. ## Using a trait A generic serializer that branches on whether a type is trivially copyable, an enum, a container or a pointer uses traits in four typical places: 1. **`static_assert`** rejects unsupported types with a message: `static_assert(!std::is_pointer_v<T>, "serialize the pointee, not the address");`. 2. **`if constexpr`** (C++17) picks a code path: `if constexpr (std::is_enum_v<T>)`. 3. **Template arguments** compute types: `std::underlying_type_t<Kind>` names the integer type to write for an enum. 4. **Tag dispatch** passes `std::is_trivially_copyable<T>{}` to overloads taking `std::true_type` and `std::false_type`. Put together, the first lines of such a serializer read like its specification: ```cpp template <class T> void write(std::vector<unsigned char>& out, const T& value) { static_assert(!std::is_pointer_v<T>, "write the pointee, not the address"); if constexpr (std::is_enum_v<T>) write(out, static_cast<std::underlying_type_t<T>>(value)); else if constexpr (std::is_trivially_copyable_v<T>) { const auto* p = reinterpret_cast<const unsigned char*>(&value); out.insert(out.end(), p, p + sizeof(T)); } // ... } ``` Every condition is decided per instantiation; the generated code for `write<int>` contains only the byte-copy path. ## Common traits by category - **Primary categories**: `is_void`, `is_integral`, `is_floating_point`, `is_array`, `is_pointer`, `is_enum`, `is_class`, `is_function` and six more; the full set of fourteen is designed so that exactly one is true for any type. - **Composite categories**: `is_arithmetic`, `is_scalar`, `is_object`, `is_reference`. - **Properties**: `is_const`, `is_trivially_copyable`, `is_signed`, `is_scoped_enum` (C++23). - **Relationships**: `is_same`, `is_base_of`, `is_convertible`. - **Transformations**: `remove_const`, `remove_reference`, `remove_cvref` (C++20), `decay`, `conditional`, `underlying_type`. ## Answers that surprise - `std::is_same_v<int, const int>` is `false`: cv-qualifiers are part of the type. - `std::is_pointer_v<char[4]>` is `false`: an array is not a pointer, even though it converts to one; a pointer to member is not a pointer either. - `std::is_integral_v<bool>` and `std::is_integral_v<char>` are `true`; a serializer that treats integers as numbers must exclude them explicitly if it means "numbers". - `std::is_trivially_copyable_v<std::string>` is `false`, so a byte-copy path correctly refuses it. - `std::is_enum_v<const Kind&>` is `false`: a reference type is not an enumeration type, so strip references before asking. - `std::is_pod` is deprecated (it is listed among the deprecated features since C++20); ask the precise question instead, usually `std::is_trivially_copyable` or `std::is_standard_layout`. ## Do not specialize the standard traits The standard says that, unless a trait is documented otherwise, a program that adds specializations of the templates in `<type_traits>` has **undefined behaviour**. To mark your own `BigInt` as integer-like, write your own trait or concept instead. Apple clang 21 with libc++ goes further and rejects `template <> struct std::is_integral<BigInt> : std::true_type {};` outright with `'is_integral' cannot be specialized: Users are not allowed to specialize this standard library entity`.

  • Can you specialize std::is_integral so that your BigInt counts as an integer type?
    No. Unless a trait says otherwise, adding specializations of the `<type_traits>` templates is undefined behaviour, and other library code relies on `std::is_integral` meaning the built-in integer types. Apple clang 21 with libc++ rejects it with a `-Winvalid-specialization` error. Define your own trait, such as `is_integer_like<T>`, and specialize that.
  • Why do value traits derive from std::integral_constant instead of just declaring a static bool?
    The base gives every trait the same interface: `::value`, `value_type`, a conversion to `bool` and a call operator. It also makes each answer a distinct type, `std::true_type` or `std::false_type`, so a trait object can drive overload selection in tag dispatch, and your own traits can inherit the answer from an existing one.

  • Type traits are evaluated at run time, like typeid.
  • std::is_integral_v<T> is a type whose ::value you must read.
  • The _t aliases arrived in C++17 together with the _v helpers.
  • Specializing std::is_integral is the right way to mark a custom number type.
  • std::is_same_v<int, const int> is true because qualifiers are ignored.