From: Jonathan Wakely Date: Thu, 27 Feb 2025 15:48:49 +0000 (+0000) Subject: libstdc++: Add static_assertions to ranges::to adaptor factory [PR112803] X-Git-Tag: releases/gcc-14.3.0~347 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=679af7b1037ae8fd4fe3f7b22ba03673b1af8c98;p=thirdparty%2Fgcc.git libstdc++: Add static_assertions to ranges::to adaptor factory [PR112803] 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. (cherry picked from commit a9cfcd0d9e3780c71442057f636f62a7142056cb) --- diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index c2dcb0265660..ede042c54fc4 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -9546,6 +9546,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 index 000000000000..0a73b0200b0c --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/conv/112803.cc @@ -0,0 +1,20 @@ +// { dg-do compile { target c++23 } } + +// Bug 112803 - : to(Args&&... args) is missing Mandates + +#include + +void +test() +{ + struct C { }; + + (void) std::ranges::to(); // { dg-error "here" } + (void) std::ranges::to(); // { dg-error "here" } + (void) std::ranges::to(); // { dg-error "here" } + (void) std::ranges::to(); // { dg-error "here" } + (void) std::ranges::to(); // { dg-error "here" } + (void) std::ranges::to(); // { dg-error "here" } +} + +// { dg-error "static assertion failed" "" { target *-*-* } 0 }