To wygląda jak błąd GCC. Standard mówi (9.5p2):
At most one non-static data member of a union may have a brace-or-equal-initializer.
W przeciwnym razie zasady są takie same jak w przypadku zwykłej klasy.
EDIT: Dodatkowo 12.6.2p8:
In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then
- if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;
- otherwise, if the entity is a variant member (9.5), no initialization is performed;
- otherwise, the entity is default-initialized (8.5).
Przypuszczalnie domyślnie zdefiniowany konstruktor domyślny zlicza tutaj. Członek i
spełnia kryteria w pierwszym punkcie wypunktowania, więc został zainicjowany tak, jakby był zwykłym członkiem klasy. Element s
pasuje do drugiego punktu wypunktowania, więc pozostaje niezainicjowany.
+1 Dobre pytanie. – Nawaz
[klang] (http://coliru.stacked-crooked.com/view?id=6ab1a0f46b2229dfc46dfa6f4f5e3163-6e9f1f680880347f6708b805c806db62) wydaje się być zadowolony z twojego kodu. – Praetorian
@Praetorian, Dzięki. Jednak nie wstawiłeś instrukcji wyjściowych. Więc tutaj jest poprawne [wyjście klang] (http://coliru.stacked-crooked.com/view?id=542928082d0e3f7d6edd525a7fcfac44-6e9f1f680880347f6708b805c806db62). – iammilind