Date: Fri, 26 Jan 2024 12:27:29 +0100
Hi Jan,
I am not saying that automatic storage variables are currently initialized at compile-time.
I am saying that constinit variables are currently (potentially) initialized at compile-time (alternatively they are intialized dynamically before main()). And as far as I understood it is a defining feature for constinit that this is possible. Otherwise one would really just use constexpr.
The question is, how to mix automatic storage variables with constinit.
In the motivating example you write that portions are computed at compile-time, other portions at run-time.
So the object is mutable and the (first?) initialization (construction) happens during compile-time?
The three main issues are
- if portions happen during compile-time, are they stored in writable memory? Outside of the stack? Or are they moved?
- the automatic storage variable is not necessarily unique any longer (compared to current usage of 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)
Sebastian
-----Ursprüngliche Nachricht-----
Von:Jan Schultke <janschultke_at_[hidden]>
Gesendet:Fr 26.01.2024 12:03
Betreff:Re: [std-proposals] Expanding constinit
An:std-proposals_at_[hidden];
CC:Sebastian Wittmeier <wittmeier_at_[hidden]>;
> The constinit would (at least potentially) happen during compile-time instead?
You seem to misunderstand something here. This is impossible.
Even constexpr merely implies that the initializer can be evaluated at
compile-time. However, the initialized object is always a new object,
which is initialized at run-time. For example, if we write:
> constexpr int x = 10 + 5;
This guarantees that the evaluation of the initializer 10 + 5 can take
place at compile-time. However, each time the surrounding function is
called, a new object x with value 15 is initialized, at run-time.
With that in mind, I don't see any reason why constinit shouldn't be
permitted for variables with automatic storage duration. It could
simply mean "constexpr, but the object is mutable once initialized".
Consider the following motivating example:
> constinit config c{ .buffer_size = compute_size(...) };
> c.host_name = get_host_name_from_os();
> start_service(c);
In this example, portions of c are computed at compile-time, and other
portions are added later at run-time.
Received on 2024-01-26 11:27:31