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.
March 30th, 2021 at 2:10 am
For this reason all your papers are checked thoroughly by the latest plagiarism detection softwares before delivery.
March 30th, 2021 at 2:11 am
The standards of education have improved significantly since students are not only competing with their peers but people from across the globe.
March 30th, 2021 at 3:35 am
To balance this pressure is not an easy task.
March 30th, 2021 at 3:39 am
The purpose of an online assignment maker is not limited to writing assignments but also providing any academic assistance that the student might require.
March 30th, 2021 at 3:43 am
The price of the assignment is always directly proportional to the quality of the work that one demand.
March 30th, 2021 at 11:57 pm
Although this may be true, IT has always been the preferred hub to search for an assignment maker Australia by the students.
March 31st, 2021 at 12:04 am
However, an unparalleled quality that you get at our organisation is unmatched anywhere else.
March 31st, 2021 at 12:05 am
There is a wide range of assignments that a student can encounter.
March 31st, 2021 at 12:13 am
Engineering is one of the oldest professions and course of study in the world.
March 31st, 2021 at 12:39 am
The high regard of engineers never goes out of fashion.
March 31st, 2021 at 12:43 am
That is why, he keeps searching for assignment making website where he can get his assignments solved at minimal rate possible.
March 31st, 2021 at 12:45 am
Some of the best assignment maker in engineering domain are available here to help you
March 31st, 2021 at 12:55 am
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.
March 31st, 2021 at 12:57 am
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.
March 31st, 2021 at 1:04 am
For all these, our assignment maker online is your trusted destination.
March 31st, 2021 at 1:06 am
Many law assignment makers are associated with My Assignment Services offering assignment help in various courses of law.
March 31st, 2021 at 1:17 am
They efficiently decipher, analyse, process and execute the law to solve any legal problem provided.
March 31st, 2021 at 1:20 am
The best assignment maker offering law assignment help with us write full-fledged criminal law assignments.
March 31st, 2021 at 1:32 am
They explain the ideologies of a criminal law efficiently with the specified format to the audience
March 31st, 2021 at 1:33 am
They prepare answers using their critical analysis skills and make rational decisions while applying any law to an ethical problem.
August 25th, 2021 at 2:51 am
Fill up your travel map with the help of Things to do post – seize the opportunity to discover the world around you.