https://gcc.gnu.org/g:3b8a67b0cfc072f027eab24fb72d48d10cc890b4

commit r14-10636-g3b8a67b0cfc072f027eab24fb72d48d10cc890b4
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Tue Aug 20 16:52:22 2024 +0100

    libstdc++: Fix std::variant to reject array types [PR116381]
    
    For the backport, rejecting array types is only done in strict modes.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/116381
            * include/std/variant (variant): Fix conditions for
            static_assert to match the spec.
            * testsuite/20_util/variant/types_neg.cc: New test.
    
    (cherry picked from commit 1e10b3b8825ee398f077500af6ae1f5db180983a)

Diff:
---
 libstdc++-v3/include/std/variant                    | 11 +++++++----
 libstdc++-v3/testsuite/20_util/variant/types_neg.cc | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
index 4e56134a6f7..834bb548f13 100644
--- a/libstdc++-v3/include/std/variant
+++ b/libstdc++-v3/include/std/variant
@@ -1374,10 +1374,13 @@ namespace __variant
 
       static_assert(sizeof...(_Types) > 0,
                    "variant must have at least one alternative");
-      static_assert(!(std::is_reference_v<_Types> || ...),
-                   "variant must have no reference alternative");
-      static_assert(!(std::is_void_v<_Types> || ...),
-                   "variant must have no void alternative");
+#ifdef __STRICT_ANSI__
+      static_assert(((std::is_object_v<_Types> && !is_array_v<_Types>) && ...),
+                   "variant alternatives must be non-array object types");
+#else
+      static_assert((std::is_object_v<_Types> && ...),
+                   "variant alternatives must be object types");
+#endif
 
       using _Base = __detail::__variant::_Variant_base<_Types...>;
 
diff --git a/libstdc++-v3/testsuite/20_util/variant/types_neg.cc 
b/libstdc++-v3/testsuite/20_util/variant/types_neg.cc
new file mode 100644
index 00000000000..7d970e961c2
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/variant/types_neg.cc
@@ -0,0 +1,18 @@
+// { dg-do compile { target c++17 } }
+// { dg-add-options strict_std }
+
+# include <variant>
+
+std::variant<> v0; // { dg-error "here" }
+// { dg-error "must have at least one alternative" "" { target *-*-* } 0 }
+std::variant<int, void> v1; // { dg-error "here" }
+std::variant<int, const void> v2; // { dg-error "here" }
+std::variant<int, int&> v3; // { dg-error "here" }
+std::variant<int, void()> v4; // { dg-error "here" }
+std::variant<int, int[]> v5; // { dg-error "here" }
+std::variant<int, int[1]> v6; // { dg-error "here" }
+// { dg-error "must be non-array object types" "" { target *-*-* } 0 }
+
+// All of variant's base classes are instantiated before checking any
+// static_assert, so we get lots of errors before the expected errors above.
+// { dg-excess-errors "" }

Reply via email to