I'am learning C++ so I'am currently reading the book "C++ Primer".
I was reading some examples about the "constexpr variables" and I just want to try a basic code that I just wrote but it doesn't compile I don't know why.
This is the code :
#include <iostream> using namespace std; int j = 8; const int *p = &j; int main() { constexpr int i = *p; // *p should be 8. return 0; } The compiler says : "the value of 'p' is not usable in a constant expression"
If I replace the "constexpr" by "const" no problem at all but I think that because the value of *p should be known at compile time there shouldn't be any problems.
I don't know where I made a mistake. (Please be tolerant my first language is French)
jandpare not constexpr, so how canibe constexpr?main(e.g. a global constructor) that modified the value ofj?constexpr int j = 8;andconstexpr const int *p = &j;and it should work.