]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix std::variant to reject array types [PR116381]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 20 Aug 2024 15:52:22 +0000 (16:52 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 21 Aug 2024 08:48:57 +0000 (09:48 +0100)
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.

libstdc++-v3/include/std/variant
libstdc++-v3/testsuite/20_util/variant/types_neg.cc [new file with mode: 0644]

index 12108d07f0b1fc90d6f0345cdc8a2069993a4895..5fb7770d889112c07f816d13f0f30f12e4b6b3e8 100644 (file)
@@ -1457,10 +1457,8 @@ namespace __detail::__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");
+      static_assert(((std::is_object_v<_Types> && !is_array_v<_Types>) && ...),
+                   "variant alternatives must be non-array object types");
 
       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 (file)
index 0000000..5cd3d02
--- /dev/null
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++17 } }
+
+# 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 "" }