User-defined Literals in C++11

The new C++11 language offers many convenient new features. The auto and constexpr keywords, variadic templates, lambda expressions, uniform initialization, and the improved standard library are often mentioned, but there are many other less well-known changes. Since the LLVM/Clang and GCC compilers now support the full C++11 language, I decided to take a closer look at the language specification and begin using some of the new features. In the process, I stumbled upon a feature that evidently hasn't received much publicity: user-defined literals. Among other things, this feature allows you to deal with unit conversions in a much more natural way. There's an in-depth discussion here. I have summarized the unit conversion usage in the simple, artificial example below.

It has long been common to use type definitions or custom classes to keep track of units. For example, if I were working on a project that dealt with temperature, I would likely define the type, kelvin to store temperature values,
typedef float kelvin;
Using the kelvin type to store temperatures makes the code easier to understand and, in some cases, it allows the compiler to find potential errors. Suppose that I need to hard-code a few temperature constants into the project for some reason. Further suppose that all of the constants that I am entering were written down in degrees Fahrenheit rather than kelvin. With C++98, I would have either converted the temperatures to kelvin before entering them into the code or, more likely, I would have written a short function to convert from degrees Fahrenheit to kelvin and then used the function to do my conversions. With the new user-defined literals, I would do the following:
constexpr kelvin operator"" _F(long double T)
    return 0.55555556 * (T - 32.0) + 273.15; 
Then, writing _F after a number, as in 129.4_F, would have the same effect as calling a function that returns kelvin,
So, the output of
const kelvin min_temperature = 129.4_F;

std::cout << min_temperature << std::endl;
would be 327.2611.
Furthermore, it could also be useful to also define _K as
constexpr kelvin operator"" _K(long double T)
    return T;
Then, whenever I enter a temperature that is measured in kelvin, I would append _K to clarify the meaning to anyone reading the code (and also to the compiler). It gets even better; suppose kelvin is a class that can be initialized with a floating point number. If an overloaded function overloaded() had two different forms—overloaded(float) and overloaded(kelvin)—entering overloaded(239.1) would call the float form, while overloaded(239.1_K) would call the kelvin form. With C++98, we would have had to use a cast, as in
overloaded( static_cast<kelvin>(239.1) ) 
or at least write
overloaded( kelvin(239.1) ) 
Once again, this has been an artificial example. The general method illustrated here can be very useful in more complicated, real-world situations. Also, I have only shown a very specific example of the "cooked" form of user-defined literals for floating point data types. There are also "raw" forms. Refer to the links at the beginning of this post for more information.

9 Responses to “User-defined Literals in C++11”

  1. H3g3m0n Says:

    I would recommend using type rich programming and defining your own types rather than typedefing a float. You have to do things like implement your own comparison operators but then you can do things like:

    auto temp1 = Celsius(108.3);
    auto temp2 = 34.5_c;
    auto temp3 = 55.8_f
    auto temp4 = temp1 + temp2 + temp3; // All the conversions are done magically

    You can have a look at

  2. Walme Says:

    When looking for an MBA essay editing service, make sure that you look at a few things. First of all, it can cost a pretty penny, so it's important to shop around before you settle on anything. accounting assignment help

  3. CathyRoberson Says:

    Very important topic, probably the most important of all ... And I really like your way you write Very helpful article with plenty of good tips. Voted up, useful,and interesting. Good work.

  4. Academic Papers Says:

    Awesome. Can we use these literals in object oriented programming using C++?

  5. Isitonline Says:

    Great.I like this C++ language.It is an important topic that must be learned by every programmer.

  6. Merlin John Says:

    First of all I would like to thank the blog for sharing the features of this new C++11 language. I really found it informative and helpful. I was searching for the details of the same and I am glad to receive this well researched article from your side.
    Manufactured Home

  7. Safeer Says:

    Teacher’s training is the key to success for a school, college or university to flourish its strategies and approaches. Training generates new and effective planning which leads to explore innovative ideas. We academic writers list the basic ideas for best study.

  8. Rashmi Says:

    Tanks for posting these user defined literals.It helped me a lot.

    Thank you
    Global Zest BS
    Global zest business solution is a service which helps in
    generating leads for B2B clients. This global zest business
    solution service covered all parts of USA. This service
    mainly emphasis on E-mail marketing. It helps in finding
    an enormous amount of clients for a business.

  9. gmail qa Says:

    Thank you for sharing the post! Glad to find the information.
    gmail email login

Leave a Reply