constexpr specifier

The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time.

Example

1
2
3
4
5
6
7
8
9
10
#include <iostream>

constexpr int accumSum(int n) {
if (n == 1) return 1;
return n + accumSum(n-1);
}

int main () {
int a = accumSum(10); // evaluate during runtime
}

Try you code on compiler explorer

On the right side, the assembly code shows that the program calls accumSum(int) function explicitly during runtime.

If we add constexpr when declaring a, we can see the function call does not appear in the assembly code.

const vs constexpr

  • const can be deferred at runtime.
  • constexpr must be evaluated at compile time. All constexpr variables are const.
1
2
3
4
5
constexpr int x = 1; // OK
constexpr int y = 2 * 3; // OK
constexpr int z; // Error
int i = 0;
constexpr int j = i + 1; // Error, i is not constexpr

constexpr in OOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Base {
private:
int m_i;
public:
constexpr Base(int i) : m_i(i) {}

// A read-only function, can not change any non-static data or call any member functions that aren't constant
constexpr int getVal() const {
return m_i;
}
};

int main() {
constexpr Base b(5);
constexpr int x = b.getVal();
std::cout << "x: " << x << std::endl;
}

References

Author

Joe Chu

Posted on

2023-11-15

Updated on

2024-03-24

Licensed under

Comments