Why constexpr should be static?

Viewed 102

After reading this and this I still feel confused about this kind of expressions:

static constexpr int = 0;

AFAIK, in C++:

  • static ensures life-time memory address along whole execution and safe initialization with concurrent threads
  • constexpr ensures time-compilation evaluation as rvalue, which means it shall have no memory address

They look contradictory to me. static ensures the variable will have a long-time memory address whereas constexpr ensures the opposite assumption. Surprisingly, the discussion in the first link mentions this:

constexpr int x = 3;
const int* p = &x;

How can we even obtain the memory address of x if it is an rvalue?

Could anyone explain it?

1 Answers

static has a number of meanings. In classes (per your comment), it means that the member is associated with the class, and not a specific instance (object) of that class.

For a constexpr, that makes a lot of sense. That's typically initialized by a value known to the compiler, and not from ctor arguments.

Related