]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add static_assert to std::integer_sequence [PR112473]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 10 Nov 2023 12:21:52 +0000 (12:21 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 18 Mar 2024 13:50:54 +0000 (13:50 +0000)
C++20 allows class types as non-type template parameters, but
std::integer_sequence explicitly disallows them. Enforce that.

libstdc++-v3/ChangeLog:

PR libstdc++/112473
* include/bits/utility.h (integer_sequence): Add static_assert.
* testsuite/20_util/integer_sequence/112473.cc: New test.

(cherry picked from commit 0953497a81f1e320989b9f2aaa7f56747eddd4a0)

libstdc++-v3/include/bits/utility.h
libstdc++-v3/testsuite/20_util/integer_sequence/112473.cc [new file with mode: 0644]

index 180866e3581ab9910d095863d432678650bde8e1..6483a8e537edd040fff77e9e36e82e58795e70df 100644 (file)
@@ -163,6 +163,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp, _Tp... _Idx>
     struct integer_sequence
     {
+#if __cplusplus >= 202002L
+      static_assert(is_integral_v<_Tp>);
+#endif
       typedef _Tp value_type;
       static constexpr size_t size() noexcept { return sizeof...(_Idx); }
     };
diff --git a/libstdc++-v3/testsuite/20_util/integer_sequence/112473.cc b/libstdc++-v3/testsuite/20_util/integer_sequence/112473.cc
new file mode 100644 (file)
index 0000000..14abfbc
--- /dev/null
@@ -0,0 +1,8 @@
+// { dg-do compile { target c++20 } }
+
+// PR libstdc++/112473 - integer_sequence accepts non-integer types
+
+#include <utility>
+
+std::integer_sequence<std::pair<int, int>, std::pair<int, int>{0, 0}> ic;
+// { dg-error "static assertion failed" "" { target *-*-* } 0 }