C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Expanding constinit

From: Jan Schultke <janschultke_at_[hidden]>
Date: Fri, 26 Jan 2024 12:43:08 +0100
> So the object is mutable and the (first?) initialization (construction) happens during compile-time?

No, the initialization takes place at run-time. You probably
understand it conceptually but you're not putting it into the right
words. The evaluation of the initializer may take place at
compile-time, but the initialization takes place at run-time, both for
constexpr and constinit. Even static constexpr objects are initialized
at run-time; namely when the program starts and their object
representation is loaded from the executable. There is no such thing
as "initialized at compile-time". This would imply that the object
exists outside of time and space, for all eternity, on all machines,
once you compile.

> if portions happen during compile-time, are they stored in writable memory? Outside of the stack? Or are they moved?

This is up to the implementation. The standard doesn't mandate that
any variable is put onto the stack, or any particular memory section
for that matter. Storage duration somewhat restricts implementation
options.

> the automatic storage variable is not necessarily unique any longer (compared to current usage of constinit)

This is an orthogonal issue. Storage duration is dictated by static or
thread_local, not by constinit.

> the time of construction (if parts are done during compile-time) is not, what programmers are used for scoped variables, perhaps one can be sure that there is no effective difference as everything is constant-evaluated (pure functions)

The time of construction is not something that is affected by
constexpr or hypothetically constinit for automatic storage duration.
It's still constructed when control passes through it.

Received on 2024-01-26 11:43:20