My last post “C++20: The Core Language” presented the new features of the C++20 core language. Today, I continue my journey with an overview of the C++20 library.
The image shows you my plan for today.
Calendar and Time-Zone
The chrono library from C++11/14 was extended with a calendar and time-zone facility. If you don’t know the Chrono library, read my posts to time.
Calendar: consists of types, which represent a year, a month, a day of a weekday, and an n-th weekday of a month. These elementary types can be combined into complex types such for example year_month, year_month_day, year_month_day_last, years_month_weekday, and year_month_weekday_last. The operator “/” is overloaded for the convenient specification of time points. Additionally, we will get new with C++20 literals: d for a day and y for a year.
Time points can be displayed in various specific time zones.
Due to the extended chrono library, the following use cases are easy to implement:
- representing dates in various forms
- get the last day of a month
- get the number of days between two dates
- printing the current time in various time-zones
Of course, C++20 uses the std::chrono namespace instead of the date namespace. Here is the output of the program:
A std::span is an object that can refer to a contiguous sequence of objects. A std::span, sometimes also called a view, is never an owner. This contiguous memory can be an array, a pointer with a size, or a std::vector. A typical implementation needs a pointer to its first element and a size. The main reason for having a std::span<T> is that a plain array will decay to a pointer if passed to a function; therefore, the size is lost. std::span<T> automatically deduces the size of the plain array or the std::vector. If you use a pointer to initialize a
std::span<T>, you must provide the constructor’s size.
In contrast to the function copy_n (1), copy (2) doesn’t need the number of elements. Hence, a common cause of errors is gone with std::span<T>.
C++ becomes more and more constexpr. For example, many algorithms of the Standard Template Library get with C++20 a constexpr overload. constexpr for a function or function template means that it could be performed at compile time. The question is now, which containers can be used at compile time? With C++20, the answer is std::string and std::vector.
Before C++20, both could not be used in a constexpr evaluation because of three limiting aspects.
- Destructors couldn’t be constexpr.
- Dynamic memory allocation/deallocation wasn’t available.
- In-place construction using placement-new wasn’t available.
These limiting aspects are now solved.
Point 3 talks about placement-new, which is quite unknown. Placement-new is often used to instantiate an object in a pre-reserved memory area. Besides, you can overload placement-new globally or for your data types.
Here are the steps to use placement-new. The first line allocates memory for an Account, which is used in the second line to construct an account in place. Admittedly, the expression account->~Account() looks strange. This expression is one of these rare cases in which you must explicitly call the destructor. Finally, the last line frees the memory.
I will not go further into the details of constexpr Containers. If you are curious, read proposal 784R1.
cppreference.com/concisely describes the new formatting library: “The text formatting library offers a safe and extensible alternative to the printf family of functions. It is intended to complement the existing C++ I/O streams library and reuse some of its infrastructure such as overloaded insertion operators for user-defined types.”. This concise description includes a straightforward example:
Maybe, this reminds you of Pythons format string. You are right. There is already an implementation of the std::format on GitHub available: fmt. Here are a few examples from the mentioned implementation. Instead of std, it uses the namespace fmt.
- Format and use positional arguments
- Convert an integer to a string in a safe way
- Format user-defined types
As promised, I will dive deeper into a future post in the library. But first, I have to finish my high-level overview of C++20. My next post is about the concurrency features.
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,