liveBook · Manning

User-defined literal

As explained in Learn C++ by Example

A user-defined literal is a literal whose suffix invokes a programmer-defined or library-provided literal operator to produce a specified type.

A user-defined literal attaches a suffix to a literal and invokes a literal operator that produces a stronger, specified type. Chrono uses 23h for hours, 34min for minutes, and 1s for seconds instead of constructor syntax such as hours{23}. section 4.2.3

Readable duration syntax

The suffix h calls an operator such as hours operator""h(long long _Val), which returns hours(_Val). Enable the appropriate chrono-literal namespace, for example with using namespace std::literals::chrono_literals. Similar library suffixes support complex numbers with i and strings with s. section 4.2.3

auto nearly_a_day = 23h;

Compare concise literal syntax with constructor syntax when defining a duration. section 4.2.3

How Learn C++ by Example covers it

How deep it goes
Explains suffix invocation, a representative operator implementation, namespace visibility, and supported chrono units.
Examples
Uses 23h, 34min, 1s, operator""h, and calendar expressions such as 2022y / December / 31.
What it assumes
Assumes ordinary literal suffixes and namespace selection while contrasting them with user-defined suffix operators.
What you can do after
Evaluate readable chrono literal syntax and select the namespace needed to make it visible.
How it connects
Connects user-defined literals with chrono durations, namespaces, complex numbers, strings, and calendar types.

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.