21 questions
5
votes
1
answer
147
views
Why isn't constinit thread_local std::string possible?
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 ...
3
votes
1
answer
122
views
Structured binding declaration cannot be `constinit`
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 ...
22
votes
2
answers
2k
views
Why can I define a std::string instance that is constinit? Isn't constinit forbidden if an object requires dynamic initialization?
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:
...
1
vote
0
answers
68
views
Returning an array from a consteval function where size of an array is a function argument [duplicate]
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 ...
4
votes
3
answers
1k
views
How I place a global variable at a compile-time known address?
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 ...
1
vote
1
answer
68
views
inconsistency between compilers when getting a `constinit` reference on a template parameter object
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 ...
1
vote
1
answer
69
views
Static-storage variable initialized by calling constexpr function with constexpr arguments
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::...
2
votes
1
answer
169
views
Static data member of template class type: constexpr vs. const constinit
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 ...
1
vote
1
answer
247
views
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 ...
0
votes
0
answers
35
views
Branch with if constexpr() on constinit variables? [duplicate]
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 ...
1
vote
1
answer
145
views
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.
...
4
votes
1
answer
262
views
Why does constinit allow UB?
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' ...
0
votes
1
answer
259
views
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; // ...
2
votes
2
answers
343
views
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 ...
1
vote
2
answers
339
views
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 ...
0
votes
3
answers
433
views
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, ...
8
votes
1
answer
247
views
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 ...
2
votes
0
answers
1k
views
When to use constinit and consteval? [duplicate]
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 ...
9
votes
1
answer
1k
views
What's the real difference between "constinit" and "constexpr"? [duplicate]
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 ...
4
votes
1
answer
251
views
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)?
113
votes
2
answers
23k
views
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 ...