Questions tagged [constinit]

13 questions
88
votes
2 answers

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
  • 24,970
  • 5
  • 40
  • 69
7
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
  • 388
  • 1
  • 10
6
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
  • 39,765
  • 26
  • 162
  • 323
4
votes
1 answer

Why does constinit allow UB?

Say I initialize variables like this: #include constexpr uint16_t a = 65535; constinit int64_t b = a * a; // warning: integer overflow in expression of type 'int' results in '-131071' [-Woverflow] constexpr int64_t c = a * a; // error:…
4
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
  • 12,564
  • 41
  • 74
2
votes
2 answers

how to guarantee initilization of a stack variable with a compile time constant

In C++20 we now have constinit. constexpr and consteval. I can now guarantee that a static variable is initialized by the result of a constexpr or consteval function by using constinit. OK I also can guarantee that a stack variable is initialized…
Klaus
  • 24,205
  • 7
  • 58
  • 113
2
votes
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
  • 21
  • 2
1
vote
1 answer

string literals as template arguments forces code duplication and verbosity

A compile-time created string is being used as a character array in a structure and its max size is driven by that string. Also that structure is being used as a member variable in another struct, and there are multiple of such string buffer structs…
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
1
vote
1 answer

Examples of constinit declaration not reachable at the point of the initializing declaration

From dcl.constinit: No diagnostic is required if no constinit declaration is reachable at the point of the initializing declaration. What does it mean? I guess an example would be sufficient. Something dynamically initialized is just ill-formed…
deshalder
  • 507
  • 2
  • 13
1
vote
2 answers

In which practical case is "const constinit" useful?

The answer to this question from @Vittorio Romeo explains constinit very well. In his answer, the following is mentioned: constexpr is not equivalent to const constinit, as the former mandates constant destruction, while the latter…
Brotcrunsher
  • 1,964
  • 10
  • 32
0
votes
0 answers

Branch with if constexpr() on constinit variables?

In the following code I try to constexpr construct a data structure and then use one of the constexpr initialized members (a_) in a method to branch with if constexpr(). From a logical standpoint AFAIK nothing speaks against it: Of course, the…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

Initialize a pointer with constinit

I was wondering whether I could initialize a pointer with constinit in C++20, and I didn't find any adequate answer on the internet. I have a simple code like this: struct a { const char *s; // pointer I want to initialize int…
dVNE
  • 161
  • 9
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
  • 41
  • 5