@@ -1499,9 +1499,24 @@ process_init_constructor_union (tree type, tree init,
constructor_elt *ce;
int len;
- /* If the initializer was empty, use default zero initialization. */
+ /* If the initializer was empty, use the union's NSDMI if it has one.
+ Otherwise use default zero initialization. */
if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
- return 0;
+ {
+ for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
+ {
+ if (DECL_INITIAL (field))
+ {
+ CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
+ field,
+ get_nsdmi (field, /*in_ctor=*/false));
+ break;
+ }
+ }
+
+ if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
+ return 0;
+ }
len = CONSTRUCTOR_ELTS (init)->length ();
if (len > 1)
new file mode 100644
@@ -0,0 +1,33 @@
+// PR c++/70347
+// { dg-do compile { target c++14 } }
+
+union A {
+ char a;
+ long b = -42;
+};
+
+struct B {
+ union {
+ char a = 10;
+ long b;
+ };
+};
+
+A c1{};
+A c2{4};
+B c3{};
+B c4{{9}};
+
+int main() {
+ if (c1.b != -42)
+ __builtin_abort ();
+
+ if (c2.a != 4)
+ __builtin_abort ();
+
+ if (c3.a != 10)
+ __builtin_abort ();
+
+ if (c4.a != 9)
+ __builtin_abort ();
+}