if constexpr (C++17)
In a Nutshell
Selectively compile a branch at compile time based on a constant condition inside templates. Discarded branches need not even pass syntax checking -- a powerful tool for compile-time polymorphism.
Header
None (language feature)
Core API Quick Reference
| Syntax | Description |
|---|---|
if constexpr (cond) { ... } | If cond is true, compile the then-branch |
if constexpr (cond) { ... } else { ... } | Compile one of two branches |
if constexpr (cond1) { ... } else if constexpr (cond2) { ... } else { ... } | Multi-branch chain |
if constexpr with type traits | if constexpr (std::integral <T> ) -- type characteristic check |
if constexpr with requires | (C++20) Prefer concept-based overloads instead |
Minimal Example
cpp
// Standard: C++17
#include <iostream>
#include <type_traits>
template <typename T>
auto print_type(const T& val) {
if constexpr (std::is_integral_v<T>) {
std::cout << "integral: " << val << "\n";
} else if constexpr (std::is_floating_point_v<T>) {
std::cout << "float: " << val << "\n";
} else {
std::cout << "other\n";
}
}
int main() {
print_type(42); // integral: 42
print_type(3.14); // float: 3.14
print_type("hi"); // other
}Embedded Applicability: High
- Zero runtime overhead: conditions are evaluated at compile time; unsatisfied branches generate no code at all
- Replaces SFINAE and tag dispatch, dramatically improving template metaprogramming readability
- Ideal for selecting different code paths based on compile-time constants such as hardware platform or peripheral type
- Available since C++17; supported by GCC 7+ and ARM Clang 6+
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 7 | 3.9 | 19.1 |
See Also
Some content adapted from cppreference.com under CC-BY-SA 4.0 license