if constexpr
The lesson explains why 'if constexpr' should be used, and how it is different from simple if statements.
We'll cover the following...
This is a big one!
The compile-time if
for C++!
The feature allows you to discard branches of an if
statement at compile-time based on a constant
expression condition.
Press + to interact
if constexpr (cond)statement1; // Discarded if cond is falseelsestatement2; // Discarded if cond is true
For Example:
Press + to interact
template <typename T>auto get_value(T t){if constexpr (std::is_pointer_v<T>)return *t;elsereturn t;}
if constexpr
has the potential to simplify a lot of template code - especially when tag dispatching,
SFINAE or preprocessor techniques are used.
...