C++ 11 feature: constexpr

constexpr” is a new concept in C++ 11.

In this chapter we shall have a look at “constexpr” topic.

“constexpr” are used in the scenario, where there is a possibility to evaluate the value of the function or variable at compile time.

“constexpr” is the keyword used to create a constant expression.

Constant expression can be used on functions, conditional statements and also in constructors.

But there are some conditions to use them.

Rules for constexpr for variable:

1. The type must be a literal type.
2. The constructor parameters must contain only literal type.

Rules for constexpr for a function:

1. Function sould not be a virtual function.
2. Must return a literal type.
3. “constexpr” function can call another “constexpr” function, but not a normal function.

Example for a simple constexpr function:

#include <iostream>
//for more C++ tutorial visit www.ProDeveloperTutorial.com

using namespace std;

constexpr int add (int a, int b)
{
    return a+b;
}

int main()
{
    int a;
    int b;
    
    cout<<"Enter the values of a and b"<<endl;
    cin>> a >>b;
    
    cout<<add(a,b)<<endl;
    
    cout<<add(10,20)<<endl;
    
    return 0;
}

Output:

Enter the values of a and b
1 2

3
30

As you can see in the above example, the line “cout<<add(10,20)<<endl;”, the result can be known as compile time.

But the value for “cout<<add(a,b)<<endl;”m the value cannot be known at compile time. Because at runtime the user can provide values to a and b.

So this means, when there is a possibility to know the result at compile time, even if the compilation time is more, during runtime the execution is faster.

But only by doing so will not make the program faster. To make sure that the “add(10, 20)” statement is evaluated at compile time, you should store the result at constexpr variable.

i.e If you store it like “constexpr int result = add(10, 20);”. This will make sure that the expression is evaluated at compile time only.

Example for constexpr in if statement

In the below example, we shall check if the size of “int” datatype is greater than 4 or not using “constexpr”

#include <iostream>
//for more C++ tutorial visit www.ProDeveloperTutorial.com

using namespace std;

int main()
{
    if constexpr (sizeof(int) > 4)
    {
        cout<<"Size is greater than 4"<<endl;
    }
    else
    {
        cout<<"Size is lesser than 4"<<endl;
    }
    
    return 0;
}

Output:

Size is lesser than 4

Example for constexpr with constructor:

#include <iostream>
//for more C++ tutorial visit www.ProDeveloperTutorial.com

using namespace std;
class MyClass 
{ 
    int a, b; 
public:

    // constexpr constructor 
    constexpr MyClass (int num1, int num2) : a(num1), b(num2) {} 
      
    constexpr int getSum ()  {   return (a+b); } 
}; 

int main() 
{ 
    // Below object is initialized at compile time 
    constexpr MyClass obj(10, 20); 
    cout << obj.getSum()<<endl; 
    return 0; 
} 

Output:

30
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *