The if constexpr Statement

Learn how to use the if constexpr statement in template functions.

if constexpr in template functions

The if constexpr statement allows template functions to evaluate different scopes in the same function at compile time. Take a look at the following example, where a function template called speak() tries to differentiate member functions depending on the type:

Get hands-on with 1400+ tech skills courses.