In today’s post, I want to introduce an exciting C++17 feature:
constexpr if. constexpr if enables it to compile source code conditionally and can be used for nice tricks at compile time.
constexpr if is straightforward.
The code snippet shows one exciting fact about
constexpr if: Although it is called
constexpr if, it is used as
if constexpr (std::is_pointer_v<T>).
T is a pointer, the if branch in line (1) will be compiled. If not, the else branch in line (2). Two points are important to mention. The function
getValue has two different return types, and both branches of the
if statement have to be valid.
The expression in constexpr if has to be a compile-time predicate. A compile time predicate is a function that returns a boolean and runs at compile time. I used in the code snippet a function from the type-traits library. Alternatively, in C++20, you can use a concept. Here is the equivalent example using the concept
I see, the two code snippets are not so impressive. Let me continue with template metaprogramming.
constexpr if, template metaprogramming is often easier to write and read.
Template Metaprogramming with
Metaprogramming is programming on programs. C++ applies metaprogramming at compile time. It started in C++98 with template metaprogramming, was formalized in C++11 with the type-traits library, and since C++11 has steadily improved.
Here is the “Hello World” of template metaprogramming: calculating the factorial of a number:
factorial<5>::value (line 1) causes the instantiation of the primary or general template (line 2). During this instantiation,
Factorial<4>::value will be instantiated. This recursion will end if the fully specialized class template
Factorial<1> kicks in (line 3).
If you want to know more about template metaprogramming, read my previous posts:
- Template Metaprogramming – How it All Started
- Template Metaprogramming – How it Works
- Template Metaprogramming – Hybrid Programming
Let me rewrite the program using
The primary template of
Factorial (line 1) becomes the if condition in the
factorial (line 3), and the full specialization of
Factorial for 1 (line 2) becomes the else case in the
constexpr function factorial (line 3). Of course, the class template
Factorial and the
factorial return the same result and are executed at compile time (line 4). To make it short, I prefer the constexpr function using
constexpr if because it reads almost such as a usual function.
Let’s do it once more. Here is the infamous Fibonacci function-based template metaprogramming (
constexpr if (fibonacci).
fibonacci is straightforward to read. The entire functionality is in one function body. In contrast, the template metaprogram
Fibonacci requires three classes. The primary template (line 1) and the two full specializations for the values 1 and 0 (lines 2 and 3).
More Information about my Mentoring Program, “Fundamentals for C++ Professionals”
I created the platform for my new mentoring on https://www.modernescpp.org/. You can skip through each of the 28 lessons. I also presented the 6th lesson about move semantics and perfect forwarding in the post ‘More Information about my Mentoring Program “Fundamentals for C++ Professionals”‘. Here are the next steps before I start the mentoring program.
- Beginning of March: online information session about my mentoring program, where you can also ask your questions
- Middle of March: my mentoring program opens for registration
- April: registration for the mentoring program closes, and the mentoring program starts
If you want to stay informed, write an e-mail to info@ModernesCpp.de with the subject “Mentoring”. Write me also an e-mail if you need more information.
Templates are a powerful tool and, therefore, provide new design choices. In my next post, I will write about static and dynamic polymorphism.
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, and Bhavith C Achar.
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,