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.

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

  1. assignment writers Says:

    For this reason all your papers are checked thoroughly by the latest plagiarism detection softwares before delivery.

  2. thesis writers Says:

    The standards of education have improved significantly since students are not only competing with their peers but people from across the globe.

  3. assignment writers Says:

    To balance this pressure is not an easy task.

  4. thesis writers Says:

    The purpose of an online assignment maker is not limited to writing assignments but also providing any academic assistance that the student might require.

  5. assignment writers Says:

    The price of the assignment is always directly proportional to the quality of the work that one demand.

  6. thesis writers Says:

    Although this may be true, IT has always been the preferred hub to search for an assignment maker Australia by the students.

  7. assignment writers Says:

    However, an unparalleled quality that you get at our organisation is unmatched anywhere else.

  8. thesis writers Says:

    There is a wide range of assignments that a student can encounter.

  9. assignment writers Says:

    Engineering is one of the oldest professions and course of study in the world.

  10. thesis writers Says:

    The high regard of engineers never goes out of fashion.

  11. assignment writers Says:

    That is why, he keeps searching for assignment making website where he can get his assignments solved at minimal rate possible.

  12. thesis writers Says:

    Some of the best assignment maker in engineering domain are available here to help you

  13. assignment writers Says:

    They have certification courses of many software like AutoCAD, CATIA, SolidWorks, ANSYS, MATLAB and know how to improve the efficiency of a shop floor by implementing wok-motion study and improving individual effectiveness with Kaizens.

  14. thesis writers Says:

    We have started our services of providing assignment maker in Melbourne and from then we have expanded to not only whole of Australia, but also keep covering major countries of the world including South Africa, USA, Canada, New Zealand, UK, Philippines, Sri Lanka, Malaysia, Kenya, Qatar, Singapore, and many more.

  15. assignment writers Says:

    For all these, our assignment maker online is your trusted destination.

  16. thesis writers Says:

    Many law assignment makers are associated with My Assignment Services offering assignment help in various courses of law.

  17. assignment writers Says:

    They efficiently decipher, analyse, process and execute the law to solve any legal problem provided.

  18. thesis writers Says:

    The best assignment maker offering law assignment help with us write full-fledged criminal law assignments.

  19. assignment writers Says:

    They explain the ideologies of a criminal law efficiently with the specified format to the audience

  20. thesis writers Says:

    They prepare answers using their critical analysis skills and make rational decisions while applying any law to an ethical problem.

Leave a Reply