Skip to main content
Filter by
Sorted by
Tagged with
5 votes
1 answer
147 views

The following code is not possible to compile. I'm wondering why: #include <string> constinit std::string constinit_string; // WORKS constinit thread_local int my_int; // WORKS constinit ...
Zebrafish's user avatar
  • 16.3k
3 votes
1 answer
122 views

Can one declare a structured binding with constinit specifier starting from C++20? For example struct A { int i, j; }; constinit auto [x, y] = A{ 0, 1 }; Here compilers somewhat diverge. MSVC ...
Fedor's user avatar
  • 24.7k
22 votes
2 answers
2k views

The cppreference mentions that if initialization contains a dynamic initialization part, the program is ill-formed when using constinit in C++20. I just wonder why the following code compiles then: ...
Ijaz Ahmad's user avatar
1 vote
0 answers
68 views

Why is it not possible to create an array in a consteval function (and return it) if array size is not a function template argument but just a function argument? The argument must be known at compile ...
Rudolf Lovrenčić's user avatar
4 votes
3 answers
1k views

I'm writing code for a specific stm32 chip with memory mapped peripherals. I happen to know that one of the GPIO ports, GPIOA, is controlled by several 32 bit ints located starting at address ...
Filipp's user avatar
  • 2,082
1 vote
1 answer
68 views

In a previous post I discussed a technic to create static values from non-type template parameters. In this post and its accepted answer, a code was given to achieve this goal but it fails to compile ...
Oersted's user avatar
  • 3,834
1 vote
1 answer
69 views

I have the following infinitely recursive constexpr function: constexpr int foo(int x) { return foo(x + 1); } Then I found that int main() { static int x = foo(5); static int y = std::...
GKxx's user avatar
  • 638
2 votes
1 answer
169 views

I have a class: #include <array> template<class T, std::size_t N> requires std::is_arithmetic_v<T> && (N >= 1) class Vector { static constexpr std::size_t ...
seanlego23's user avatar
1 vote
1 answer
247 views

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 ...
YePhIcK's user avatar
  • 5,876
0 votes
0 answers
35 views

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 ...
glades's user avatar
  • 5,374
1 vote
1 answer
145 views

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. ...
ledonter's user avatar
  • 1,769
4 votes
1 answer
262 views

Say I initialize variables like this: #include <cstdint> constexpr uint16_t a = 65535; constinit int64_t b = a * a; // warning: integer overflow in expression of type 'int' results in '-131071' ...
Goswin von Brederlow's user avatar
0 votes
1 answer
259 views

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; // ...
dVNE's user avatar
  • 161
2 votes
2 answers
343 views

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 ...
Klaus's user avatar
  • 26k
1 vote
2 answers
339 views

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 ...
Brotcrunsher's user avatar
  • 2,304
0 votes
3 answers
433 views

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, ...
HowP's user avatar
  • 41
8 votes
1 answer
247 views

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 ...
Helmut Zeisel's user avatar
2 votes
0 answers
1k views

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 ...
ashubhatt's user avatar
9 votes
1 answer
1k views

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 ...
xmllmx's user avatar
  • 44.6k
4 votes
1 answer
251 views

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's user avatar
  • 14.4k
113 votes
2 answers
23k views

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 ...
Acorn's user avatar
  • 26.4k