liveBook · Manning

Long literal

As explained in Learn C++ by Example

A literal suffix is a token appended to a literal to determine or customize the type or interpretation of its value.

A literal suffix attaches meaning to a number by converting it into a stronger chrono type. Instead of writing hours{23}, write 23h; the suffix makes duration code easier to read. section 4.2.3

Chrono provides suffixes for several time units:

  • 12h, 34min, and 1s represent hours, minutes, and seconds. section 4.2.3
  • 1ms, 1us, and 1ns represent milliseconds, microseconds, and nanoseconds. section 4.2.3
  • 2023y and 23d represent a calendar year and day; named months such as December avoid numeric month ambiguity. section 4.2.3

Readable date construction

A date can combine calendar literals with operator/: year_month_day{2022y, December, 31d} or 2022y / December / 31. These forms expose the intended year, month, and day more clearly than numeric construction. section 4.2.3

hours operator""h(long long _Val) {
   return hours(_Val);
}

The h suffix is implemented through operator""h, which receives a number and returns a chrono hours value. A suitable chrono literal namespace must be visible. section 4.2.3

How Learn C++ by Example covers it

How deep it goes
Explains suffix syntax, supported units, namespaces, and operator implementation without developing custom literal definitions.
Examples
Uses duration forms such as 23h and date forms such as 2022y / December / 31.
What you can do after
Apply it via chrono literal suffixes to write strongly typed durations and readable calendar dates.
How it connects
Connects user-defined literals with chrono durations, calendar types, namespaces, and operator/.

Also explained in

Compare how 2 books explain Long literal →

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.