From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362]
Date: Wed, 8 Mar 2023 09:35:27 -0500 [thread overview]
Message-ID: <20230308143527.113337-1-ppalka@redhat.com> (raw)
Tested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 12?
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.
---
libstdc++-v3/include/std/ranges | 40 ++++++++++++++-----
.../testsuite/std/ranges/iota/iota_view.cc | 10 +++++
.../testsuite/std/ranges/istream_view.cc | 12 ++++++
.../testsuite/std/ranges/single_view.cc | 13 ++++++
4 files changed, 65 insertions(+), 10 deletions(-)
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 0a65d74bb5b..67566c6ebcf 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{};
+ namespace __detail
+ {
+ template<typename _Tp>
+ concept __can_single_view
+ = requires { single_view<decay_t<_Tp>>(std::declval<_Tp>()); };
+ } // namespace __detail
+
struct _Single
{
- template<typename _Tp>
- [[nodiscard]]
+ 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,22 @@ 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);
+ };
+ };
+
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, std::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();
}
--
2.40.0.rc0.57.g454dfcbddf
next reply other threads:[~2023-03-08 14:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-08 14:35 Patrick Palka [this message]
2023-03-09 17:52 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230308143527.113337-1-ppalka@redhat.com \
--to=ppalka@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).