From 88e2378c8a9a45d4617d9386619677038b392ff1 Mon Sep 17 00:00:00 2001 From: Alan de Freitas Date: Mon, 23 Feb 2026 22:56:04 -0500 Subject: [PATCH] Add -Wmaybe-uninitialized pragma to union storage constructors GCC 7+ produces false-positive -Wmaybe-uninitialized warnings at -O3 when deeply inlining through union constructors that initialize one member while another (dummy_) exists. This is the same class of false positive addressed in boostorg/variant2#55. The pragmas are placed around the forwarding constructors in both constexpr_union_storage_t and fallback_union_storage_t. --- .../boost/optional/detail/union_optional.hpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/boost/optional/detail/union_optional.hpp b/include/boost/optional/detail/union_optional.hpp index 46daecba..16e150f4 100644 --- a/include/boost/optional/detail/union_optional.hpp +++ b/include/boost/optional/detail/union_optional.hpp @@ -74,9 +74,20 @@ union constexpr_union_storage_t constexpr constexpr_union_storage_t( trivial_init_t ) noexcept : dummy_() {}; +#if defined(BOOST_GCC) && (__GNUC__ >= 7) +// false positive, see https://github.com/boostorg/variant2/issues/55, +// https://github.com/boostorg/url/issues/979 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + template constexpr constexpr_union_storage_t( Args&&... args ) : value_(forward_(args)...) {} +#if defined(BOOST_GCC) && (__GNUC__ >= 7) +# pragma GCC diagnostic pop +#endif + //~constexpr_union_storage_t() = default; // No need to destroy a trivially-destructible type }; @@ -88,9 +99,20 @@ union fallback_union_storage_t constexpr fallback_union_storage_t( trivial_init_t ) noexcept : dummy_() {}; +#if defined(BOOST_GCC) && (__GNUC__ >= 7) +// false positive, see https://github.com/boostorg/variant2/issues/55, +// https://github.com/boostorg/url/issues/979 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + template constexpr fallback_union_storage_t( Args&&... args ) : value_(forward_(args)...) {} +#if defined(BOOST_GCC) && (__GNUC__ >= 7) +# pragma GCC diagnostic pop +#endif + ~fallback_union_storage_t(){} // My owner will destroy the `T` if needed. // Cannot default in a union with nontrivial `T`. };