On terça-feira, 12 de novembro de 2013 17:36:03, Richard Smith wrote:
> > Shouldn't compilers emit then a diagnostic if you fail to sequence them in
> > the
> > right order?
>
> Sure, they can, just as they do for constructor initializers. And, just as
> with constructor initializers, we need to sequence the subobject
> initialization in subobject order, in order to get destructors to run in
> the right order.
Good.
>
> > struct S {
> >
> > int a, b;
> >
> > };
> >
> > int i;
> > int f(int *i) { *i = 42; return 47; }
> >
> > struct S g()
> > {
> >
> > struct S s = { .b = f(i), .a = i };
>
> I assume you meant f(&i) here?
Yes. I had a reference first, then I changed to a pointer so I could compile in
C mode...
> return s;
>
> > }
> >
> > When compiled as C99, the above returns { .a = 42, .b = 47 }.
>
> Only by chance. C99 6.7.8/23 says: "The order in which any side effects
> occur among the initialization list expressions is unspecified."
Ah, ok. So my code actually has undefined behaviour. For C++, though, we do
have an order and we also have to initialise sub-objects in the correct order.