const vs constexpr in C++

CMP
4 min readSep 15, 2023

Both const and constexpr are keywords used to specify that a value cannot be modified, but they have some differences in functionality and use cases.

What is const?

const is a fundamental keyword in C++ used for defining constant variables, meaning the value of the variable cannot change. Look at the following simple example of this concept:

int main() {
const int a = 0;
a = 5; // error

return 0;
}

In addition to constant variables, const can be used for member functions. Look at the following class:

--

--

CMP

Software engineer specializing in operating systems, navigating the intracicies of the C++ language and systems programming.