This post concludes my presentation of library features in C++20. Today I am writing about the class
std::source_location and a few functions for bit manipulation.
std::source_location represents information about the source code. This information includes file names, line numbers, and function names. The information is precious when you need information about the call site for debugging, logging, or testing purposes. The class
std::source_location is the better alternative for the predefined C++11 macros
__LINE__ and should, therefore, be used.
The following table shows the interface of
std::source_location::current() creates a new source location object
src. src represents the information of the call site. Now, no C++ compiler supports
std::source_location. Consequently, the following program
sourceLocation.cpp is from cppreference.com/source_location.
The output of the program is part of its source code.
C++20 makes it quite comfortable to access or manipulate bits or bit sequences.
Thanks to the new type std::endian, you get the endianness of a scalar type.
- Endianness can be big-endian or little-endian. Big-endian means the most significant byte comes first; little-endian means that the least significant byte comes first.
- A scalar type is either an arithmetic type, an
enum, a pointer, a member pointer, or a
endian provides the endianness of all scalar types:
- If all scalar types are little-endian,
std::endian::nativeis equal to
- If all scalar types are big-endian,
Even corner cases are supported:
- If all scalar types have
sizeof1 and therefore endianness does not matter; the values of the enumerators
- If the platform uses mixed endianness,
std::endian::nativeis neither equal to
When I perform the following program
getEndianness.cpp on an x86 architecture, I get the answer little-endian.
Accessing or Manipulating Bits or Bit Sequences
The following table gives you the first overview of all functions.
The functions except
std::bit_cast require an unsigned integer type (
unsigned char, unsigned short, unsigned int, unsigned long, or
unsigned long long).
bit.cpp shows the usage of the functions.
Here is the output of the program:
The next program shows the application and the output of the functions
std::bit_popcount for the numbers 2 to 7.
Additionally to coroutines, C++20 has much to offer for concurrency. First, C++20 has new atomics. The new atomics exist for floating-point values and smart pointers. C++20 also enables waiting on atomics. To coordinate threads, semaphores, latches, and barriers come into play. Also, the
std::thread was improved with
std::jthread. The execution of a
std::jthread can be interrupted and joins automatically in its destructor.
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, Marco Parri Empoli, moon, and Philipp Lenk.
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|
|My special thanks to SHAVEDYAKS|
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,