Sujet : Re: c initialization algorithm
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 15. Oct 2024, 21:30:43
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20241015131416.301@kylheku.com>
References : 1 2 3 4 5 6
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-10-15, Thiago Adams <
thiago.adams@gmail.com> wrote:
int main() {
struct X x = {.i = f(), .i = f() };
}
https://godbolt.org/z/rf984cGMM
There is a warning in this sample and f is called just once.
Forgot to say that I didn't find this part on the standard.
I think initialization is also very superficial on books.
The current public draft, which I think is still N3047, does
address the issue of repeated mentions of the same subobject in
a designated initializer:
The initialization shall occur in initializer list order, each
initializer provided for a particular subobject overriding any
previously listed initializer for the same subobject.(184)
Since the second .i = f() overrides the previously listed .i = f(),
there is only one initialization of .i.
It is not specified whether both calls to f() occur; i.e.
is the initializing expression of the overridden initializer still
evaluated?
The referenced footnoted 184 address itself to this question.
But not only is the footnote not normative text, as you know,
it uses the phrase "might not", thus only clarifying that this
aspect is not specified. f could be called twice or only once:
184. Any initializer for the subobject which is overridden and
so not used to initialize that subobject might not be evaluated at
all.
Don't write code that initializes subobjects more than once, or else if
you do, don't have side effects in the initializing expressions such
that the program depends on all of them being invoked.
The implementation's warning is a good idea.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca