Questions tagged [constinit]

6 questions
56
votes
1 answer

What is `constinit` in C++20?

constinit is a new keyword and specifier in C++20 which was proposed in P1143. The following example is provided in the standard: const char * g() { return "dynamic initialization"; } constexpr const char * f(bool p) { return p ? "constant…
Acorn
  • 22,093
  • 4
  • 30
  • 62
6
votes
1 answer

When is the destructor of a constinit object called?

Generally it is said that the destructors of static objects are called in the reverse order of the constructors. As I understand, constinit objects are initialized at compile time, so their destructors should be called after the destructors of…
Helmut Zeisel
  • 326
  • 1
  • 10
3
votes
1 answer

What's the real difference between "constinit" and "constexpr"?

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…
xmllmx
  • 33,981
  • 13
  • 121
  • 269
3
votes
1 answer

Can C++20 `constinit` waive the need for nifty counter idiom?

C++20 introduced constinit to avoid static initialization order fiasco. Can constinit waive the need for the nifty counter idiom (e.g. for initialization of std::cout)?
Amir Kirsh
  • 8,021
  • 22
  • 43
1
vote
0 answers

When to use constinit and consteval?

The usage of constexpr is quite straightforward which is to make sure the code can be evaluated at the compile time itself. But the latest features of C++ 20 which provides constinit and consteval are little bit confusing. Can someone provide the…
ashubhatt
  • 11
  • 1
0
votes
3 answers

static_assert fails while using constinit const. Confusion in constinit, constinit const, constexpr, const, nonconst variables

I have a question about compile time functions. I understand that static_assert should work only with types, that can be evaluated/computed at compile time. So it does not work with std::string (yet, no support in gcc10 for constexpr std::string)…
HowP
  • 31
  • 5