diff --git a/libcxx/docs/OneRangesProposalStatus.csv b/libcxx/docs/OneRangesProposalStatus.csv --- a/libcxx/docs/OneRangesProposalStatus.csv +++ b/libcxx/docs/OneRangesProposalStatus.csv @@ -40,7 +40,7 @@ [ranges.primitives],"size, empty, data, and cdata",[iterator.concepts],Zoe Carver,, [range.range],,[range.access],,, [range.sized],,"[range.primitives], [range.range]",,, -[range.view],View and enable_view,[range.range],,, +[range.view],View and enable_view,[range.range],Louis Dionne,https://reviews.llvm.org/D101547,✅ [range.refinements],"OutputRange, InputRange, ForwardRange, BidirectionalRange, RandomAccessRange, ContiguousRange, CommonRange, ViewableRange","[ranges.syn]: pt. 2, [range.range]",Christopher Di Bella,"input_range: `D100271 `_ forward_range: `D100275 `_ bidirectional_range: `D100278 `_", diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -41,6 +41,7 @@ __ranges/concepts.h __ranges/end.h __ranges/enable_borrowed_range.h + __ranges/view.h __split_buffer __sso_allocator __std_stream diff --git a/libcxx/include/__ranges/view.h b/libcxx/include/__ranges/view.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__ranges/view.h @@ -0,0 +1,51 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___RANGES_VIEW_H +#define _LIBCPP___RANGES_VIEW_H + +#include <__config> +#include <__ranges/concepts.h> +#include + + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_HAS_NO_RANGES) + +namespace ranges { + +struct view_base { }; + +template +inline constexpr bool enable_view = derived_from<_Tp, view_base>; + +template +concept view = + range<_Tp> && + movable<_Tp> && + default_initializable<_Tp> && + enable_view<_Tp>; + +} // end namespace ranges + +#endif // !_LIBCPP_HAS_NO_RANGES + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___RANGES_VIEW_H diff --git a/libcxx/include/ranges b/libcxx/include/ranges --- a/libcxx/include/ranges +++ b/libcxx/include/ranges @@ -47,6 +47,15 @@ template using range_rvalue_reference_t = iter_rvalue_reference_t>; + // [range.view], views + template + inline constexpr bool enable_view = ...; + + struct view_base { }; + + template + concept view = ...; + // [range.refinements], other range refinements template concept common_range = see below; @@ -59,9 +68,9 @@ #include <__ranges/cbegin.h> #include <__ranges/cend.h> #include <__ranges/concepts.h> -#include <__ranges/end.h> #include <__ranges/enable_borrowed_range.h> #include <__ranges/end.h> +#include <__ranges/view.h> #include // Required by the standard. #include // Required by the standard. #include // Required by the standard. diff --git a/libcxx/test/std/ranges/range.view/enable_view.compile.pass.cpp b/libcxx/test/std/ranges/range.view/enable_view.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.view/enable_view.compile.pass.cpp @@ -0,0 +1,48 @@ +//===----------------------------------------------------------------------===// +// +// 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-no-concepts +// XFAIL: msvc && clang + +// + +// template +// inline constexpr bool enable_view = ...; + +#include + +#include "test_macros.h" + +// Doesn't derive from view_base +struct Empty { }; +static_assert(!std::ranges::enable_view); + +// Derives from view_base, but privately +struct PrivateViewBase : private std::ranges::view_base { }; +static_assert(!std::ranges::enable_view); + +// Derives from view_base, but specializes enable_view to false +struct EnableViewFalse : std::ranges::view_base { }; +namespace std::ranges { template <> constexpr bool enable_view = false; } +static_assert(!std::ranges::enable_view); + + +// Derives from view_base +struct PublicViewBase : std::ranges::view_base { }; +static_assert(std::ranges::enable_view); + +// Does not derive from view_base, but specializes enable_view to true +struct EnableViewTrue { }; +namespace std::ranges { template <> constexpr bool enable_view = true; } +static_assert(std::ranges::enable_view); + + +// Make sure that enable_view is a bool, not some other contextually-convertible-to-bool type. +ASSERT_SAME_TYPE(decltype(std::ranges::enable_view), const bool); +ASSERT_SAME_TYPE(decltype(std::ranges::enable_view), const bool); diff --git a/libcxx/test/std/ranges/range.view/view.compile.pass.cpp b/libcxx/test/std/ranges/range.view/view.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.view/view.compile.pass.cpp @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// 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-no-concepts +// XFAIL: msvc && clang + +// + +// template +// concept view = ...; + +#include + +#include "test_macros.h" + +// The type would be a view, but it's not moveable. +struct NotMoveable : std::ranges::view_base { + NotMoveable() = default; + NotMoveable(NotMoveable&&) = delete; + NotMoveable& operator=(NotMoveable&&) = delete; + friend int* begin(NotMoveable&); + friend int* begin(NotMoveable const&); + friend int* end(NotMoveable&); + friend int* end(NotMoveable const&); +}; +static_assert(std::ranges::range); +static_assert(!std::movable); +static_assert(std::default_initializable); +static_assert(std::ranges::enable_view); +static_assert(!std::ranges::view); + +// The type would be a view, but it's not default initializable +struct NotDefaultInit : std::ranges::view_base { + NotDefaultInit() = delete; + friend int* begin(NotDefaultInit&); + friend int* begin(NotDefaultInit const&); + friend int* end(NotDefaultInit&); + friend int* end(NotDefaultInit const&); +}; +static_assert(std::ranges::range); +static_assert(std::movable); +static_assert(!std::default_initializable); +static_assert(std::ranges::enable_view); +static_assert(!std::ranges::view); + +// The type would be a view, but it doesn't enable it with enable_view +struct NotExplicitlyEnabled { + NotExplicitlyEnabled() = default; + NotExplicitlyEnabled(NotExplicitlyEnabled&&) = default; + NotExplicitlyEnabled& operator=(NotExplicitlyEnabled&&) = default; + friend int* begin(NotExplicitlyEnabled&); + friend int* begin(NotExplicitlyEnabled const&); + friend int* end(NotExplicitlyEnabled&); + friend int* end(NotExplicitlyEnabled const&); +}; +static_assert(std::ranges::range); +static_assert(std::movable); +static_assert(std::default_initializable); +static_assert(!std::ranges::enable_view); +static_assert(!std::ranges::view); + +// The type has everything else, but it's not a range +struct NotARange : std::ranges::view_base { + NotARange() = default; + NotARange(NotARange&&) = default; + NotARange& operator=(NotARange&&) = default; +}; +static_assert(!std::ranges::range); +static_assert(std::movable); +static_assert(std::default_initializable); +static_assert(std::ranges::enable_view); +static_assert(!std::ranges::view); + +// The type satisfies all requirements +struct View : std::ranges::view_base { + View() = default; + View(View&&) = default; + View& operator=(View&&) = default; + friend int* begin(View&); + friend int* begin(View const&); + friend int* end(View&); + friend int* end(View const&); +}; +static_assert(std::ranges::range); +static_assert(std::movable); +static_assert(std::default_initializable); +static_assert(std::ranges::enable_view); +static_assert(std::ranges::view); diff --git a/libcxx/test/std/ranges/range.view/view_base.compile.pass.cpp b/libcxx/test/std/ranges/range.view/view_base.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.view/view_base.compile.pass.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// 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-no-concepts +// XFAIL: msvc && clang + +// + +// struct view_base { }; + +#include +#include + +static_assert(std::is_empty_v); +static_assert(std::is_trivial_v); + +// Make sure we can inherit from it, as it's intended (that wouldn't be the +// case if e.g. it was marked as final). +struct View : std::ranges::view_base { };