Home

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

|
Updated:  
2016-03-26 08:29:51
|
C++ For Dummies
Explore Book
Buy On Amazon

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.

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Jeff Cogswell has been an application developer and trainer for 18 years, working with clients from startups to Fortune 500 companies. He has developed courses on C++ and other technologies.