3
constexpr int f() { return 0; }
int g() { return 0; }

constexpr auto c1 = f(); // OK
constinit auto c2 = f(); // OK

constexpr auto d1 = g(); // ill-formed
constinit auto d2 = g(); // ill-formed

int main() {}

As illustrated in the code above, I cannot find any difference between constinit and constexpr.

What's the real difference between constinit and constexpr?


Update:

The related What is constinit in C++20? doesn't clearly state the difference between constinit and constexpr.

xmllmx
  • 33,981
  • 13
  • 121
  • 269
  • @E_net4thedupefinder, the answer in your link doesn't clearly state the difference between `constinit` and `constexpr`. – xmllmx Jun 18 '20 at 07:30
  • 1
    Yes, it does. The very first answer goes into explicit detail about what `constexpr` entails and how `constinit` does not imply most of those things. – Nicol Bolas Jun 18 '20 at 13:28

1 Answers1

5

A constinit variable is constant initialized, but it is not usable in a constant expression, nor even automatically constant. Your main can legally contain this line

c2 = 2; 

Yup, modification is possible after initialization.

StoryTeller - Unslander Monica
  • 148,497
  • 21
  • 320
  • 399