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,
_F(129.4);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.
October 19th, 2024 at 11:52 pm
Just wish to say your article is as surprising The clearness in your post is just cool and i could assume youre an expert on this subject Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post Thanks a million and please keep up the enjoyable work
October 24th, 2024 at 11:30 pm
Aroma Sensei I do not even understand how I ended up here, but I assumed this publish used to be great
October 25th, 2024 at 4:08 am
Jinx Manga Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
November 7th, 2024 at 9:10 am
Your own blue sex lagoon
lisa ann com
November 8th, 2024 at 8:56 am
mine dating
lisa ann mom
November 12th, 2024 at 5:04 pm
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?