Learn How To Use User Defined Literal Operators In Modern C++

C++11 introduced new forms of literals using modified syntax and semantics in order to provide User-Defined Literals (UDL) also known as Extensible Literals. Using user-defined literals, user-defined classes can provide new literal syntax and they can be used with the operator `""` to combine values with conversion operators. In this post, we explain how to use user-defined literals in modern C++.

What is a user-defined literal in C++?

Literals are the constant values that are assigned to the constant variables and C++11 introduced new forms of literals using modified syntax and semantics in order to provide user-defined literals (UDL), aka Extensible Literals. Using user-defined literals, user-defined classes can provide new literal syntax. User-defined literals combine values with conversion operators.

User-defined literals can be useful when doing some mathematical conversions, i.e. “the degree to radian”, “radian to a degree”, “centimeter to meter”, “inch to meter”, “kg to milligram”, etc. Literals are good for understanding conversions, for example, we can use “_RAD” for the radian value for degrees and when we use “30_RAD” we understand it is the radian value for 30 degrees.

How to use user-defined literal operators in C++?

In modern C++ User-defined literals are defined by the operator”” that is treated as a call to a literal operator. In this concept, only suffix forms are supported. The name of the literal operator is operator ”“ followed by the suffix, generally preferred followed by the “_” character. Here is a general syntax for the user-defined literals,

Here is a user defined literal operator example that converts degree to radians.

and we can use this `_RAD` literal as below:

As you see, in this line, this is clear that we convert 30 degrees to radian. Personally, I like `short` values but if you want more specific, you can use _ToRAD, _DEGTORAD, _Deg_To_Rad, etc.

Another benefit of literals is they are useful to use formulas are in standard forms i.e. American, English forms, ISO, DIN, SI metric systems etc. As in this example you can use same conversion for all SI units in math, physics and engineering applications. In example, you can use metric formulas in calculations and you can use literal _inch to use an inch value.

Is there an example of how to use user-defined literal operators in C++?

Here is a simple user defined literal operator example in C++.

Here is another example that converts inch to cm.

For more information on this UDL feature, see User-defined literals Proposal document.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋It’s nice to meet you.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.