Understanding constexpr if and compile-time branching in C++
This medium-difficulty question tests your grasp of constexpr if, a C++17 feature that enables compile-time conditional branching. It requires you to reason about which code paths are instantiated, type-checked, and executed—and to distinguish between what a function *appears* to do and what it actually does at runtime.
The core skill being probed is whether you understand that constexpr if allows the compiler to discard entire branches based on a compile-time constant condition, affecting not just performance but also which symbols are even defined or type-checked. Extensional equivalence—whether two functions produce identical outputs for all inputs—depends on whether both code paths are reachable and produce the same results, or whether one path is eliminated entirely during compilation.
- Compile-time vs. runtime evaluation
- Template instantiation and discarded statements
- Type checking in active vs. inactive branches
- Behavioural equivalence under different input scenarios