Trusted answers to developer questions

What is the constexpr keyword in C++?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Compile-time evaluation

The constexpr keyword, short for constant expression, was a feature introduced in C++11 that allows the value of an expression to be evaluated at compile-time.

This feature significantly increases performance since the expression does not need to be computed again during runtime; i​nstead, its value can simply be used where needed.

There are three main applications of constexpr:

svg viewer

constexpr variables

The value of a variable can be computed at compile-time by making it constexpr:

constexpr int num1 = 20;
constexpr int num2 = num1 * 10;
// Both are evaluated at compile-time

The variables num1 and num2 are implicitly const. A constexpr variable has to be initilized with an expression. This expression can only contain literals or other constexpr variables.

constexpr functions

constexpr functions are executed and evaluated during compilation.

svg viewer

A constexpr function must follow the rules below:

  • Its return type cannot be void, i.e., it must compute and return a value.

  • It can only call other constexpr functions.

  • It can only refer to global const variables.

Here is an example:

#include <iostream>
using namespace std;
constexpr bool is_even(int num){
if(num % 2 == 0){
return true;
}
return false;
}
int main() {
constexpr int n = 30;
constexpr bool check_even = is_even(n); // Evaluated at compile-time
cout << check_even << endl;
}

The value of check_even is evaluated at compile-time. If this value is to remain constant throughout the program, having it computed at compile-time is better than​ computing it at runtime whenever it is required.

constexpr objects

These objects are initialized at compile-time. A constexpr object can be created by following these rules:

  • The constructor must be constexpr.

  • All non-static members must be initialized. Furthermore, they must have constexpr constructors themselves, e.g., basic data types.

  • The constructor must be defined as default or delete; otherwise, its body must be empty.

  • Object methods can also be declared constexpr as long as they follow the rules for constexpr functions.

A constexpr object has been created below:

#include <iostream>
using namespace std;
class Coordinate {
int x;
int y;
public:
// constexpr constructor
constexpr Coordinate(int x_c, int y_c): x(x_c), y(y_c){}
// constexpr method
constexpr int get_x(){return x;}
constexpr int get_y(){return y;}
};
int main() {
Coordinate c = Coordinate(5, 10); // Object created at compile-time
int x = c.get_x();
int y = c.get_y(); // Coordinates retrieved at compile-time
cout << x << ", " << y << endl;
}

RELATED TAGS

Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?