Open In App

if constexpr in C++ 17

Last Updated : 25 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++17, if constexpr feature was introduced to allow compile-time branching based on constant expressions. Unlike regular if statements, which are evaluated at runtime, the if constexpr allows the compiler to discard branches of code that do not apply. It means only the branch of code for which the condition is true is compiled and other code branch is discarded during compilation.

Syntax of if constexpr

if constexpr (condition) {
    // Code executed only if condition is true at compile time
} else {
    // Code executed only if condition is false at compile time
}

Here, the condition is a constant expression to be executed at compile time. If the given condition results in true then the code inside the first block is included and if the condition results in false then the code inside the else block is included.

Note: The code inside the block which is not selected gets discarded during compilation.

Example of if constexpr

The below example demonstrates the use of if constexpr.

C++




// C++ program to demonstrate the use of if constexpr to
// print the type of value given
#include <iostream>
using namespace std;
  
// function to check for integar value
template <typename C> void printInfo(const C& value)
{
  
    // if the value is not integer, the if block will be
    // discarded
    if constexpr (is_integral_v<C>) {
        cout << "Integer Value " << value << endl;
    }
    // if the value is integer, the else block will be
    // discarded
    else {
        cout << "Non-Integer value:" << value << endl;
    }
}
  
// driver code
int main()
{
    printInfo(10);
    printInfo(3.15);
}


Output

Integer Value 10
Non-Integer value:3.15

Explanation: In the above example template function printInfo takes a template parameter C, and allows it to work with various data types. using if constexpr statement it checks at compile time if C is an integral type using is_integral_v<C>.

Application of if constexpr

The if constexpr is mainly used where decisions need to be made at compile time based on type traits or other constant expressions. Here are some key applications:

  • Template Metaprogramming: template metaprogramming can be used to create general templets as it have abilities to adapt behaviour based on conditions of compile time.
  • Conditional Code Generation: It can be used to include or exclude sections of code based on certain conditions during the compilation process.
  • Compile-Time Assertions: checking conditions using if constexpr during the process compilation of the program helps users to identify errors early and ensure that specific conditions are met at compile time.
  • Algorithm Optimization: It helps for the selection of optimized algorithms at compile time resulting optimizing performance without any runtime cost.

Difference between if constexpr() and if()

Features

if constexpr()

if()

Execution

Compile-time, the code branch that is not satisfying the condition is discarded at compile time.

Runtime, the code branch that is not satisfying the is not executed during runtime.

Condition

Condition must be a constant expressions

Condition can be any expression

Syntax Flexibility

Compile-Time Conditions

Runtime Conditions

Branching

Conditional code generation

Multiple paths at runtime

Error Reporting

Early Errors, errors related to branch that is discarded are reported during compilation.

Errors related to both discarded and non discarded branches may be reported during compilation and runtime.

Constant evaluation

Evaluated Immediately

Evaluation may be deferred

Performance

Optimized Execution

Runtime Overhead

Compile-time checks

Yes it checks at compile time

No compile time checks



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads