]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add static_assertions to ranges::to adaptor factory [PR112803]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 27 Feb 2025 15:48:49 +0000 (15:48 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 28 Feb 2025 21:35:27 +0000 (21:35 +0000)
The standard requires that we reject attempts to create a ranges::to
adaptor for cv-qualified types and non-class types. Currently we only
diagnose it once the adaptor is used in a pipeline.

This adds static assertions to diagnose it immediately.

libstdc++-v3/ChangeLog:

PR libstdc++/112803
* include/std/ranges (ranges::to): Add static assertions to
enforce Mandates conditions.
* testsuite/std/ranges/conv/112803.cc: New test.

libstdc++-v3/include/std/ranges
libstdc++-v3/testsuite/std/ranges/conv/112803.cc [new file with mode: 0644]

index 6c65722b687e439f2418be2a92562fae78451cbb..c0b1134ab328aff1625fa48c9af1ed86d6edfc1e 100644 (file)
@@ -9569,6 +9569,9 @@ namespace __detail
     constexpr auto
     to [[nodiscard]] (_Args&&... __args)
     {
+      static_assert(!is_const_v<_Cont> && !is_volatile_v<_Cont>);
+      static_assert(is_class_v<_Cont>);
+
       using __detail::_To;
       using views::__adaptor::_Partial;
       return _Partial<_To<_Cont>, decay_t<_Args>...>{0, std::forward<_Args>(__args)...};
diff --git a/libstdc++-v3/testsuite/std/ranges/conv/112803.cc b/libstdc++-v3/testsuite/std/ranges/conv/112803.cc
new file mode 100644 (file)
index 0000000..0a73b02
--- /dev/null
@@ -0,0 +1,20 @@
+// { dg-do compile { target c++23 } }
+
+// Bug 112803 - <ranges>: to(Args&&... args) is missing Mandates
+
+#include <ranges>
+
+void
+test()
+{
+  struct C { };
+
+  (void) std::ranges::to<int>(); // { dg-error "here" }
+  (void) std::ranges::to<C*>(); // { dg-error "here" }
+  (void) std::ranges::to<C&>(); // { dg-error "here" }
+  (void) std::ranges::to<const C>(); // { dg-error "here" }
+  (void) std::ranges::to<volatile C>(); // { dg-error "here" }
+  (void) std::ranges::to<const volatile C>(); // { dg-error "here" }
+}
+
+// { dg-error "static assertion failed" "" { target *-*-* } 0 }