The Need for User-Defined Literals (UDLs) in C++ - dummies

The Need for User-Defined Literals (UDLs) in C++

By John Paul Mueller, Jeff Cogswell

The whole point of literals is to make code more readable and easier to maintain. However, built-in literals are limited to a few data types, summarized as follows:

  • Integer

  • Floating-point

  • Character

  • String

  • Boolean

  • Pointer

  • UDL

Sometimes you need a literal of a type other than these built-in types and that’s where UDLs come into play. Unlike variables, the value of a UDL is always known at compile time. The compiler substitutes whatever value you define for the UDL with the actual value in the code.

The purpose of the UDL is to make the code easier for the human developer to read and understand. Once that task is completed, the compiler is free to use the actual value referenced by the UDL in the compiled code so the application doesn’t need to convert it during runtime. What this means is that your application runs faster and uses fewer resources, while remaining easy to read.

Built-in literals are straightforward because they’re based on core types. A UDL can be as complex as you need it to be to express a real-world data type. For example, if you’re involved in a field that uses imaginary numbers, you can create a UDL to fulfill that need.

You can also perform data conversions and other tasks that would be time-consuming to perform in other ways. It’s even possible to create side effects, such as performing some sort of output, using a UDL.