C++11 has user-defined literals for characters, C strings, integers, and floating-point numbers. Integers and floating-point numbers are available in raw and cooked form. Thanks to C++14, we have built-in literals for binary numbers, C++ strings, complex numbers, and time units.
The four user-defined literals
After the example in the last post user-defined literals I will provide – as promised – the details in this post. To make my intention clear, here are the literal types, including the raw and cooked variations:
How should you read the table? The data type character has the form character_suffix. An example is ‘s’_c. The compiler invokes the literal operator operator”” _c(‘s’). The character is, in this case, a char. C++ supports, in addition to the data type
char the data types wchar_t, char16_t, and char32_t. You can use this type as a base for your C string. I used in the table a
char. The table shows that the compiler maps the C string “hi”_i18 to the literal operator operator”” _i18n(“hi”,2). 2 is the length of the c string.
The compiler can map integers or floating-point numbers to integers (unsigned long long int) or floating-point numbers (long double), but the compiler can also map them to C strings. The first variant is called the cooked form; the second variant is the raw form. The compiler will use the raw form if the literal operator wants its arguments as a C string. If not, it uses the cooked form. If you implement both versions, the compiler will choose the cooked form.
Admittedly, in the last lines is a lot of confusion potential. Therefore, I sum it all up from the perspective of the signatures in the following table. The first column has the signature of the literal operator, the second column is the type of the user-defined literal, and the last column an example for a user-defined literal that fits the signature of the literal operator.
Calculate it once more
I calculated in the post user-defined literals how many meters I have to go by car on average per week. I made my calculation based on user-defined literals of the type long double in the cooked form. I have to adjust the literal operators to make my calculation in the raw form.
It’s only necessary to convert the arguments of the literal operator from type C string to long double. That is relatively easy to do with the new function std::stold.
Either I have not touched the class MyDistance.
Nor does the main program need a modification.
Of course, the result is the same.
New built-in literals with C++14
C++ added with C++14 a few new built-in literals. These are built-in literals for binary numbers, C++ strings, complex numbers, and time units. First, here is the overview.
You have to keep a few special rules in mind. The binary numbers start with the prefix 0b. The built-in literals have no underscore. That is different from the user-defined literals. C++ support with C++14 the first time a C++ string literal. So far, C++ supports only C-string literals. That means, for example, that you always have to use a C string literal to initialize a C++ string. That was very strange. The time literals are very convenient because they implicitly know their unit. They are of the type std::chrono::duration.
The base unit for time is the second. My 16-year-old son often complains that his school day is exhausting. Of course, the question arises. How many seconds does my son need for a typical school day? The program answers.
I think the program is self-explanatory. The suffixes are expressive enough. Making the correct additions is the job of the compiler. The time literals support the base arithmetic addition, subtraction, multiplication, division, and modulo operation.
I have no C++14-compliant compiler at my disposal. Not really an issue. The online compiler on en.cppreference.com gives me the answers
My son needs 27300 seconds for all his task related to school. This is almost a typical working day in Germany of about 8 hours.
The classical enumerations (enum) in C++ have three big disadvantages.
- They convert implicitly to int.
- They introduce their enumerators into the enclosing scope.
- The type of enumerators can not be defined.
In particular, characteristics 1 and 2 are often a reason for bad surprises. The new strong-typed enumerations clear off these issues. Read about it in the next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,