liveBook · Manning

Type trait

As explained in Learn C++ by Example

A type trait is a compile-time facility that exposes a property or transformation of a C++ type.

A type trait is a compile-time facility that exposes a property or transformation of a C++ type. Traits let generic code interrogate types before execution. section 6.0 section 4.2.4

The chrono library provides std::chrono::is_clock. Its Boolean value reports whether a type satisfies the clock requirements, such as defining rep, period, duration, time_point, is_steady, and now(). section 4.2.4

Checking a clock type

std::chrono::is_clock<int>::value is false because int is not a clock. Applying the trait can expose an unsuitable type before another time-related function uses it. section 4.2.4

std::chrono::is_clock<int>::value

Inspect the compact expression that queries whether int satisfies the clock requirements. section 4.2.4

How Learn C++ by Example covers it

How deep it goes
Defines traits through a clock-checking example and stops short of detailing trait implementation internals.
Examples
Demonstrates std::chrono::is_clock<int>::value as a false result for a non-clock type.
What it assumes
Assumes templates and type requirements while explaining traits through the familiar distinction between clocks and int.
What you can do after
Evaluate whether a type satisfies named requirements by reading a trait's Boolean value.
How it connects
Connects type traits with concepts, requirements, templates, chrono, and clock types.

Also explained in

Compare how 2 books explain type trait →

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.