Search⌘ K

User-Defined Literals

Explore user-defined literals in C++ and understand how adding suffixes to built-in literals allows for type-safe arithmetic and clearer code. This lesson covers syntax changes from C++11 to C++14, showing how to implement and use custom literal operators to combine values with units effectively, a key skill for safety-critical software development.

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:

C++
<built_in-Literal> + _ + <Suffix>

Usually, we use the suffix for a unit:

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

The

...