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.
This commit is contained in:
Patrick Palka 2023-03-09 13:35:04 -05:00
parent c6232ba229
commit 95827e1b9f
4 changed files with 64 additions and 10 deletions

View file

@ -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); }
};

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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();
}