constexpr
, when applied to a variable, means that you can’t change it,
like const
.
constexpr auto pi = 3.14159; cout << pi << '\n';
3.14159
constexpr double avo=6.022e23; avo = 1.234; cout << avo;
c.cc:2: error: assignment of read-only variable 'avo'
Let’s try to fool constexpr
:
constexpr int answer = 42; int *p = &answer; *p = 8675309; cout << answer << '\n';
c.cc:2: error: invalid conversion from 'const int*' to 'int*'
constexpr int answer = 42; int *p = const_cast<int *>(&answer); *p = 8675309; cout << *p << ' ' << answer << '\n';
8675309 42