...

/

User-Defined Literals

User-Defined Literals

In this lesson, we'll learn how to create user-defined literals.

We'll cover the following...

User-defined literals are a unique feature in all mainstream programming languages. They empower us to combine values with units.

widget

With C++11, it’s possible to generate user-defined literals by adding a suffix to a built-in literal for integers, floating points, characters, and C strings.

Syntax #

User-defined literals must obey the following syntax:

Press + to interact
<built_in-Literal> + _ + <Suffix>

Usually, we use the suffix for a unit:

Press + to interact
10101010_b // Natural numbers
123.45_km // Floating point numbers
"hello"_i18n // C-string literals
'1'_character // Character literals

The magic

...