C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Expanding constinit

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Fri, 26 Jan 2024 17:54:08 +0300
On 1/26/24 16:14, Jan Schultke via Std-Proposals wrote:
>> As far as the C++ standard is concerned, it is. More precisely, it
> happens before dynamic initialization and running main(), i.e. before
> any of the program's code runs.
>
> Yes, and that is still run-time. Just because it happens before
> dynamic initialization doesn't make it not run-time. The process of
> running the executable and mapping memory is obviously run-time, not
> compile-time. You're arbitrarily drawing a line before dynamic
> initialization and saying that all the run-time work the operating
> system does up to that point is not run-time ... because ...uhm?

Because all that work the OS does is not covered by the standard. From
the standard perspective, the program does not run at that point. All
program execution rules defined by the standard do not apply at that
time. Otherwise you'd be saying that the program execution starts the
moment you push the power button on your computer, which would be nonsense.

Ok, you could argue that constant initialization is formally considered
part of the program execution [basic.exec], in terms of the standard,
I'll grant you that. But for all practical intents, constant
initialization is performed by the compiler before the program is run,
in layman's terms. The entire purpose of the distinction between
constant and dynamic initialization is to guarantee that all
constant-initialized objects are available through the entire lifetime
of the program.

>> So, as far as the standard is concerned, the object could as well exist eternally.
>
> No, it would not be valid. This would imply that the object's address
> is also eternally a constant, but the address can be different each
> time the program runs.

Address of an object is not *guaranteed* to be different either. An
implementation where the object address is the same on every run would
be conforming.

> The only thing that's guaranteed is that the
> value it is initialized with AT RUN-TIME is constant.

Not the only thing. The other one is object availability, granted by the
order of initialization stages defined by the standard.

Received on 2024-01-26 14:54:11