C++C++11C++14C++17Learn C++Syntax

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

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.

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

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.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17C++20Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?

C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++