Skip to content

Commit

Permalink
[libc++] Implement LWG3549: view_interface need not inherit from view…
Browse files Browse the repository at this point in the history
…_base

Implement LWG3549 by making `view_interface` not inherit from `view_base`. Types
are still views if they have a public and unambiguous derivation from
`view_interface`, so adjust the `enable_view` machinery as such to account for
that.

Differential Revision: https://reviews.llvm.org/D117714
  • Loading branch information
JoeLoser committed Jan 23, 2022
1 parent 37d1d02 commit 2513b79
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2bIssues.csv
Expand Up @@ -91,7 +91,7 @@
`3544 <https://wg21.link/LWG3544>`__,"``format-arg-store::args`` is unintentionally not exposition-only","June 2021","","","|format|"
`3546 <https://wg21.link/LWG3546>`__,"``common_iterator``'s postfix-proxy is not quite right","June 2021","","","|ranges|"
`3548 <https://wg21.link/LWG3548>`__,"``shared_ptr`` construction from ``unique_ptr`` should move (not copy) the deleter","June 2021","",""
`3549 <https://wg21.link/LWG3549>`__,"``view_interface`` is overspecified to derive from ``view_base``","June 2021","","","|ranges|"
`3549 <https://wg21.link/LWG3549>`__,"``view_interface`` is overspecified to derive from ``view_base``","June 2021","|Complete|","14.0","|ranges|"
`3551 <https://wg21.link/LWG3551>`__,"``borrowed_{iterator,subrange}_t`` are overspecified","June 2021","","","|ranges|"
`3552 <https://wg21.link/LWG3552>`__,"Parallel specialized memory algorithms should require forward iterators","June 2021","",""
`3553 <https://wg21.link/LWG3553>`__,"Useless constraint in ``split_view::outer-iterator::value_type::begin()``","June 2021","","","|ranges|"
Expand Down
12 changes: 11 additions & 1 deletion libcxx/include/__ranges/enable_view.h
Expand Up @@ -12,6 +12,7 @@

#include <__config>
#include <concepts>
#include <type_traits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
Expand All @@ -25,8 +26,17 @@ namespace ranges {

struct view_base { };

template<class _Derived>
requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
class view_interface;

template<class _Op, class _Yp>
requires is_convertible_v<_Op*, view_interface<_Yp>*>
void __is_derived_from_view_interface(const _Op*, const view_interface<_Yp>*);

template <class _Tp>
inline constexpr bool enable_view = derived_from<_Tp, view_base>;
inline constexpr bool enable_view = derived_from<_Tp, view_base> ||
requires { ranges::__is_derived_from_view_interface((_Tp*)nullptr, (_Tp*)nullptr); };

} // end namespace ranges

Expand Down
3 changes: 1 addition & 2 deletions libcxx/include/__ranges/view_interface.h
Expand Up @@ -18,7 +18,6 @@
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__ranges/empty.h>
#include <__ranges/enable_view.h>
#include <concepts>
#include <type_traits>

Expand All @@ -40,7 +39,7 @@ void __implicitly_convert_to(type_identity_t<_Tp>) noexcept;

template<class _Derived>
requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
class view_interface : public view_base {
class view_interface {
_LIBCPP_HIDE_FROM_ABI
constexpr _Derived& __derived() noexcept {
static_assert(sizeof(_Derived) && derived_from<_Derived, view_interface> && view<_Derived>);
Expand Down
Expand Up @@ -79,6 +79,11 @@ struct MoveOnlyForwardRange : std::ranges::view_interface<MoveOnlyForwardRange>
};
static_assert(std::ranges::view<MoveOnlyForwardRange>);

struct MI : std::ranges::view_interface<InputRange>,
std::ranges::view_interface<MoveOnlyForwardRange> {
};
static_assert(!std::ranges::view<MI>);

struct EmptyIsTrue : std::ranges::view_interface<EmptyIsTrue> {
int buff[8] = {0, 1, 2, 3, 4, 5, 6, 7};
constexpr ForwardIter begin() const { return ForwardIter(const_cast<int*>(buff)); }
Expand Down Expand Up @@ -300,6 +305,10 @@ constexpr bool testFrontBack() {
return true;
}

struct V1 : std::ranges::view_interface<V1> { };
struct V2 : std::ranges::view_interface<V2> { V1 base_; };
static_assert(sizeof(V2) == sizeof(V1));

int main(int, char**) {
testEmpty();
static_assert(testEmpty());
Expand Down

0 comments on commit 2513b79

Please sign in to comment.