diff --git a/libcxx/include/__ranges/lazy_split_view.h b/libcxx/include/__ranges/lazy_split_view.h --- a/libcxx/include/__ranges/lazy_split_view.h +++ b/libcxx/include/__ranges/lazy_split_view.h @@ -87,8 +87,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr lazy_split_view(_Range&& __r, range_value_t<_Range> __e) : __base_(views::all(std::forward<_Range>(__r))) - // TODO(varconst): use `views::single` once it's implemented. - , __pattern_(ranges::single_view(std::move(__e))) {} + , __pattern_(views::single(std::move(__e))) {} _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& requires copy_constructible<_View> { return __base_; } diff --git a/libcxx/include/__ranges/single_view.h b/libcxx/include/__ranges/single_view.h --- a/libcxx/include/__ranges/single_view.h +++ b/libcxx/include/__ranges/single_view.h @@ -11,6 +11,7 @@ #include <__config> #include <__ranges/copyable_box.h> +#include <__ranges/range_adaptor.h> #include <__ranges/view_interface.h> #include <__utility/forward.h> #include <__utility/in_place.h> @@ -70,8 +71,27 @@ constexpr const _Tp* data() const noexcept { return __value_.operator->(); } }; - template - single_view(_Tp) -> single_view<_Tp>; +template +single_view(_Tp) -> single_view<_Tp>; + +namespace views { +namespace __single_view { + +struct __fn : __range_adaptor_closure<__fn> { + template + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI + constexpr auto operator()(_Range&& __range) const + noexcept(noexcept(single_view>(std::forward<_Range>(__range)))) + -> decltype( single_view>(std::forward<_Range>(__range))) + { return single_view>(std::forward<_Range>(__range)); } +}; +} // namespace __single_view + +inline namespace __cpo { + inline constexpr auto single = __single_view::__fn{}; +} // namespace __cpo + +} // namespace views } // namespace ranges #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp --- a/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp +++ b/libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp @@ -80,7 +80,7 @@ static_assert(test(std::views::iota, 1)); static_assert(test(std::views::iota, 1, 10)); //static_assert(test(std::views::istream, 1); -//static_assert(test(std::views::single, 4)); +static_assert(test(std::views::single, 4)); // [range.adaptors] static_assert(test(std::views::all, a)); diff --git a/libcxx/test/std/ranges/range.adaptors/range.lazy.split/ctor.range.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.lazy.split/ctor.range.pass.cpp --- a/libcxx/test/std/ranges/range.adaptors/range.lazy.split/ctor.range.pass.cpp +++ b/libcxx/test/std/ranges/range.adaptors/range.lazy.split/ctor.range.pass.cpp @@ -125,7 +125,6 @@ { using Range = RangeWithCounting; using Element = ElementWithCounting; - // TODO(varconst): use `views::single` once it's implemented. using Pattern = std::ranges::single_view; // Arguments are lvalues. diff --git a/libcxx/test/std/ranges/range.factories/range.single.view/cpo.pass.cpp b/libcxx/test/std/ranges/range.factories/range.single.view/cpo.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.factories/range.single.view/cpo.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-has-no-incomplete-ranges + +// std::views::single + +#include + +#include +#include +#include +#include "MoveOnly.h" + +// Can't invoke without arguments. +static_assert(!std::is_invocable_v); +// Can't invoke with a move-only type. +static_assert(!std::is_invocable_v); + +constexpr bool test() { + // Lvalue. + { + int x = 42; + std::same_as> decltype(auto) v = std::views::single(x); + assert(v.size() == 1); + assert(v.front() == x); + } + + // Prvalue. + { + std::same_as> decltype(auto) v = std::views::single(42); + assert(v.size() == 1); + assert(v.front() == 42); + } + + // Const lvalue. + { + const int x = 42; + std::same_as> decltype(auto) v = std::views::single(x); + assert(v.size() == 1); + assert(v.front() == x); + } + + // Xvalue. + { + int x = 42; + std::same_as> decltype(auto) v = std::views::single(std::move(x)); + assert(v.size() == 1); + assert(v.front() == x); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +}