A little confused about constexpr functions

Viewed 1080

Consider the following code:

constexpr const int* address(const int& r)
{
   return &r;
}


int main()
{
   int x = 10;
   constexpr const int* p = address(x); //error

   _getch();
   return 0;
}

This is an error: "function call must have a constant value in a constant expression". Ok.

Two questions:

  1. If 'x' is declared static, there are no errors. Why?

  2. How is it possible to get a variable address during the compilation process? Aren't they allocated at run-time?

5 Answers
Related