[PATCH v2] c++/reflection: bogus -Wmissing-field-initializers with <meta> [PR124950]
Marek Polacek
polacek@redhat.com
Tue Apr 21 14:49:47 GMT 2026
On Tue, Apr 21, 2026 at 07:19:13AM +0100, Jonathan Wakely wrote:
> On Tue, 21 Apr 2026, 02:43 Tim Song, <t.canens.cpp@gmail.com> wrote:
>
> > Might be better to leave out the initializer for name since not specifying
> > it is pretty unusual? That matches what Barry proposed in some version of
> > P3795 before it was removed in an LWG telecon (for lack of normative
> > effect).
> >
>
> Yes, I think that was what LWG agreed with, but couldn't say so in the
> standard wording.
That means that we'll emit a warning with
data_member_spec (^^int, { .bit_width = 8 })
which I don't like, but if that's what's been decided, here's an
updated patch.
> And would it be simpler to initialize alignment and bit_width with {}
> instead of nullopt? There's only one constructor that's viable for {} so no
> need to do resolution to find the best match for =nullopt.
Done.
Tested reflect/*, ok for trunk?
-- >8 --
We emit -Wmissing-field-initializers warnings for code like
data_member_spec (^^int, { .name = "dms" })
which seems undesirable. We can initialize the members of
std::meta::data_member_options to suppress that warning (clang's <meta>
has these initializers too).
PR c++/124950
libstdc++-v3/ChangeLog:
* include/std/meta (std::meta::data_member_options): Initialize
alignment, bit_width, and annotations members.
gcc/testsuite/ChangeLog:
* g++.dg/reflect/data_member_spec7.C: New test.
---
gcc/testsuite/g++.dg/reflect/data_member_spec7.C | 9 +++++++++
libstdc++-v3/include/std/meta | 6 +++---
2 files changed, 12 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/reflect/data_member_spec7.C
diff --git a/gcc/testsuite/g++.dg/reflect/data_member_spec7.C b/gcc/testsuite/g++.dg/reflect/data_member_spec7.C
new file mode 100644
index 00000000000..057db043bf8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/data_member_spec7.C
@@ -0,0 +1,9 @@
+// PR c++/124950
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection -Wmissing-field-initializers" }
+// Test that we don't emit bogus -Wmissing-field-initializers warnings.
+
+#include <meta>
+using namespace std::meta;
+constexpr auto dms = data_member_spec (^^int, { .name = "dms" });
+constexpr auto dms2 = data_member_spec (^^int, { .bit_width = 8 }); // { dg-warning "missing initializer" }
diff --git a/libstdc++-v3/include/std/meta b/libstdc++-v3/include/std/meta
index 22b4da44cdd..b12f49a2096 100644
--- a/libstdc++-v3/include/std/meta
+++ b/libstdc++-v3/include/std/meta
@@ -424,10 +424,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
};
optional<_Name> name;
- optional<int> alignment;
- optional<int> bit_width;
+ optional<int> alignment = {};
+ optional<int> bit_width = {};
bool no_unique_address = false;
- vector<info> annotations;
+ vector<info> annotations = {};
};
consteval info data_member_spec(info, data_member_options);
consteval bool is_data_member_spec(info);
base-commit: 6adc49fc08cb87b0e7c8ead3176b45dae2ff4e2a
--
2.53.0
More information about the Libstdc++
mailing list