public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-6560] libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362]
@ 2023-03-09 18:35 Patrick Palka
0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2023-03-09 18:35 UTC (permalink / raw)
To: gcc-cvs, libstdc++-cvs
https://gcc.gnu.org/g:95827e1b9f7d5dd5a697bd60292e3876a7e8c15c
commit r13-6560-g95827e1b9f7d5dd5a697bd60292e3876a7e8c15c
Author: Patrick Palka <ppalka@redhat.com>
Date: Thu Mar 9 13:35:04 2023 -0500
libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362]
PR libstdc++/108362
libstdc++-v3/ChangeLog:
* include/std/ranges (__detail::__can_single_view): New concept.
(_Single::operator()): Constrain it. Move [[nodiscard]] to the
end of the function declarator.
(__detail::__can_iota_view): New concept.
(_Iota::operator()): Constrain it. Move [[nodiscard]] to the
end of the function declarator.
(__detail::__can_istream_view): New concept.
(_Istream::operator()): Constrain it. Move [[nodiscard]] to the
end of the function declarator.
* testsuite/std/ranges/iota/iota_view.cc (test07): New test.
* testsuite/std/ranges/istream_view.cc (test08): New test.
* testsuite/std/ranges/single_view.cc (test07): New test.
Diff:
---
libstdc++-v3/include/std/ranges | 39 ++++++++++++++++------
.../testsuite/std/ranges/iota/iota_view.cc | 10 ++++++
libstdc++-v3/testsuite/std/ranges/istream_view.cc | 12 +++++++
libstdc++-v3/testsuite/std/ranges/single_view.cc | 13 ++++++++
4 files changed, 64 insertions(+), 10 deletions(-)
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 0a65d74bb5b..867844e9e12 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -675,30 +675,41 @@ namespace views
template<typename _Tp>
inline constexpr empty_view<_Tp> empty{};
- struct _Single
+ namespace __detail
{
template<typename _Tp>
- [[nodiscard]]
+ concept __can_single_view
+ = requires { single_view<decay_t<_Tp>>(std::declval<_Tp>()); };
+ } // namespace __detail
+
+ struct _Single
+ {
+ template<__detail::__can_single_view _Tp>
constexpr auto
- operator()(_Tp&& __e) const
+ operator() [[nodiscard]] (_Tp&& __e) const
noexcept(noexcept(single_view<decay_t<_Tp>>(std::forward<_Tp>(__e))))
{ return single_view<decay_t<_Tp>>(std::forward<_Tp>(__e)); }
};
inline constexpr _Single single{};
+ namespace __detail
+ {
+ template<typename... _Args>
+ concept __can_iota_view = requires { iota_view(std::declval<_Args>()...); };
+ } // namespace __detail
+
struct _Iota
{
- template<typename _Tp>
- [[nodiscard]]
+ template<__detail::__can_iota_view _Tp>
constexpr auto
- operator()(_Tp&& __e) const
+ operator() [[nodiscard]] (_Tp&& __e) const
{ return iota_view(std::forward<_Tp>(__e)); }
template<typename _Tp, typename _Up>
- [[nodiscard]]
+ requires __detail::__can_iota_view<_Tp, _Up>
constexpr auto
- operator()(_Tp&& __e, _Up&& __f) const
+ operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
{ return iota_view(std::forward<_Tp>(__e), std::forward<_Up>(__f)); }
};
@@ -796,13 +807,21 @@ namespace views
namespace views
{
+ namespace __detail
+ {
+ template<typename _Tp, typename _Up>
+ concept __can_istream_view = requires (_Up __e) {
+ basic_istream_view<_Tp, typename _Up::char_type, typename _Up::traits_type>(__e);
+ };
+ } // namespace __detail
+
template<typename _Tp>
struct _Istream
{
template<typename _CharT, typename _Traits>
- [[nodiscard]]
constexpr auto
- operator()(basic_istream<_CharT, _Traits>& __e) const
+ operator() [[nodiscard]] (basic_istream<_CharT, _Traits>& __e) const
+ requires __detail::__can_istream_view<_Tp, remove_reference_t<decltype(__e)>>
{ return basic_istream_view<_Tp, _CharT, _Traits>(__e); }
};
diff --git a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
index 2dd17113536..0d2eaf1d0c2 100644
--- a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
@@ -110,6 +110,15 @@ test06()
VERIFY( std::ranges::equal(v3, w3) );
}
+template<auto iota = std::views::iota>
+void
+test07()
+{
+ // Verify SFINAE behavior.
+ static_assert(!requires { iota(nullptr); });
+ static_assert(!requires { iota(nullptr, nullptr); });
+}
+
int
main()
{
@@ -119,4 +128,5 @@ main()
test04();
test05();
test06();
+ test07();
}
diff --git a/libstdc++-v3/testsuite/std/ranges/istream_view.cc b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
index 26f109fdeaa..cc1c3e006b9 100644
--- a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
@@ -115,6 +115,17 @@ test07()
VERIFY( sum == 10 );
}
+template<class T, class U>
+concept can_istream_view = requires (U u) { views::istream<T>(u); };
+
+void
+test08()
+{
+ // Verify SFINAE behavior.
+ struct S { };
+ static_assert(!can_istream_view<S, std::istringstream>);
+}
+
int
main()
{
@@ -125,4 +136,5 @@ main()
test05();
test06();
test07();
+ test08();
}
diff --git a/libstdc++-v3/testsuite/std/ranges/single_view.cc b/libstdc++-v3/testsuite/std/ranges/single_view.cc
index 063caeb8785..38a3946ca43 100644
--- a/libstdc++-v3/testsuite/std/ranges/single_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/single_view.cc
@@ -111,6 +111,18 @@ test06()
auto y = std::views::single(std::move(obj));
}
+template<auto single = std::views::single>
+void
+test07()
+{
+ // Verify SFINAE behavior.
+ struct uncopyable {
+ uncopyable();
+ uncopyable(const uncopyable&) = delete;
+ };
+ static_assert(!requires { single(uncopyable{}); });
+}
+
int main()
{
test01();
@@ -119,4 +131,5 @@ int main()
test04();
test05();
test06();
+ test07();
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-03-09 18:35 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-09 18:35 [gcc r13-6560] libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362] Patrick Palka
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).