static_assert is the tool in modern C++ to make your code safe.
The usage of static_assert is relatively easy. static_assert requires an expression and a string. The expression must be a predicate that can be evaluated at compile time. Predicate means the expression returns true or false. If the expression evaluates to false you will get at compile time an error message with the string as a message. Of course, you get no executable.
There are a few points you have to keep in your mind.
- The static_assert expression will be evaluated at compile-time, and you have no runtime overhead.
- Expressions that can be evaluated at compile time are called constant expressions. I have much to tell about the new keyword constexpr; but in a later post.
- You can use static_assert expressions in all parts of your program. Therefore, Putting general requirements on your source code in a separate header is a good idea. The result is, the static_assert expression will be automatically verified at compile time if you include the header.
Here is the code example to static_assert.
The program uses static_assert in the global scope (lines 5 and 6); it uses it in the class scope (lines 10 – line 12); uses it in the local scope (line 17). One of the two assertions in lines 5 and 6 must inevitably fall. The assertions in the class definition guarantee, on the one hand, that the number of columns and lines must be greater than 0, and it guarantees, on the other hand, that the sum has to be positive. That’s why the template instantiation in line 14 is invalid. On my computer int is more minor than long int. The C++ standard guarantees the reverse relation.
Let’s have a look at the broken run of my compiler.
The power of static_assert is due to what can be evaluated at compile time. Glad we have the new type-traits library in C++11. The powerful type-traits library empowers you to check, compare and change types at compile time. But this is the story of 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, 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,