liveBook · Manning

Type_traits header

As explained in Learn C++ by Example

The type_traits header is the C++ standard library header that declares compile-time type property queries and transformations.

The <type_traits> header lets templates inspect type properties at compile time. Each trait receives a type and exposes a Boolean result describing whether an operation is supported. section 6.1.4

Common checks include:

  • is_constructible checks whether a type can be constructed, optionally with specified argument types. section 6.1.4
  • is_default_constructible, is_destructible, and the copy or move construction and assignment traits inspect special member functions. section 6.1.4
  • is_invocable checks whether a type can be called, providing a type-trait counterpart to the invocable concept. section 6.1.4

Checking an abstract type

Include <type_traits> and use static_assert to verify Blob's properties. The _v form, introduced in C++17, directly yields the Boolean value; the older form uses ::value. section 6.1.4

Listing 6.10 Type traits to check for special member functions
#include <type_traits>
void check_properties()
{
    ...                                               #1
    static_assert(
        !std::is_default_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
         std::is_destructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
         std::has_virtual_destructor_v<Race::Blob>
    );                                                #3
}

The listing checks whether Blob is default constructible, destructible, copy constructible, copy assignable, move constructible, and move assignable. section 6.1.4

How Learn C++ by Example covers it

How deep it goes
Explains trait templates, Boolean results, parameter packs, and C++17 _v helpers, but only briefly surveys the wider trait catalogue.
Examples
Checks an abstract Blob with is_constructible, special-member-function traits, static_assert, and the _v shorthand.
What it assumes
Assumes templates, special member functions, and concepts; teaches trait syntax and Boolean results through the Blob checks.
What you can do after
Evaluate type capabilities at compile time using standard traits and static_assert.
How it connects
Connects type traits with templates, special member functions, concepts, invocable, and vector move-or-copy behavior.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.