[libcxx][ranges] Add concepts in range.utility.helpers.
authorzoecarver <z.zoelec2@gmail.com>
Tue, 1 Jun 2021 21:54:29 +0000 (14:54 -0700)
committerzoecarver <z.zoelec2@gmail.com>
Fri, 4 Jun 2021 16:56:31 +0000 (09:56 -0700)
There are no changes to public APIs.

Differential Revision: https://reviews.llvm.org/D103493

libcxx/include/__iterator/concepts.h
libcxx/include/__iterator/iterator_traits.h
libcxx/include/__ranges/view.h
libcxx/include/concepts
libcxx/test/libcxx/ranges/range.utility.helpers/different_from.compile.pass.cpp [new file with mode: 0644]
libcxx/test/libcxx/ranges/range.utility.helpers/has_arrow.compile.pass.cpp [new file with mode: 0644]
libcxx/test/libcxx/ranges/range.utility.helpers/simple_view.compile.pass.cpp [new file with mode: 0644]

index 654d27bf250bf37786482b5db35d1b033cd2b942..8b83eb1e90c0c5df7b73f921a191df5ad8bba2de 100644 (file)
@@ -168,6 +168,10 @@ concept contiguous_iterator =
     { _VSTD::to_address(__i) } -> same_as<add_pointer_t<iter_reference_t<_Ip>>>;
   };
 
+
+template<class _Ip>
+concept __has_arrow = input_iterator<_Ip> && (is_pointer_v<_Ip> || requires(_Ip __i) { __i.operator->(); });
+
 // clang-format on
 
 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
index 307ee143c1d2ff2a0f55ac22fedccf7947a3e3c6..06b199b85979e37f5558e19beb2b17f1a8afec16 100644 (file)
@@ -255,13 +255,7 @@ struct __iterator_traits_member_pointer_or_arrow_or_void<_Ip> { using type = typ
 // Otherwise, if `decltype(declval<I&>().operator->())` is well-formed, then `pointer` names that
 // type.
 template<class _Ip>
-concept __has_arrow =
-  requires(_Ip& __i) {
-    __i.operator->();
-  };
-
-template<class _Ip>
-  requires __has_arrow<_Ip> && (!__has_member_pointer<_Ip>)
+  requires requires(_Ip& __i) { __i.operator->(); } && (!__has_member_pointer<_Ip>)
 struct __iterator_traits_member_pointer_or_arrow_or_void<_Ip> {
   using type = decltype(declval<_Ip&>().operator->());
 };
index 11c0ae2db072d7eef6a28929580f7a3b00b69913..55f8928f3c59792f619c79447b316607493f71c3 100644 (file)
@@ -40,6 +40,11 @@ concept view =
   default_initializable<_Tp> &&
   enable_view<_Tp>;
 
+template<class _Range>
+concept __simple_view =
+  view<_Range> && range<const _Range> &&
+  same_as<iterator_t<_Range>, iterator_t<const _Range>> &&
+  same_as<sentinel_t<_Range>, iterator_t<const _Range>>;
 } // end namespace ranges
 
 #endif // !_LIBCPP_HAS_NO_RANGES
index 6c8f325af8afac7eb1cab128f38a50bcd6f8ecb2..0b51f53dcc3935b77a1ef8cf2c477d57764b16e1 100644 (file)
@@ -442,6 +442,9 @@ concept equivalence_relation = relation<_Rp, _Tp, _Up>;
 template<class _Rp, class _Tp, class _Up>
 concept strict_weak_order = relation<_Rp, _Tp, _Up>;
 
+template<class _Tp, class _Up>
+concept __different_from = !same_as<remove_cvref_t<_Tp>, remove_cvref_t<_Up>>;
+
 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CONCEPTS)
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/test/libcxx/ranges/range.utility.helpers/different_from.compile.pass.cpp b/libcxx/test/libcxx/ranges/range.utility.helpers/different_from.compile.pass.cpp
new file mode 100644 (file)
index 0000000..ecea0ee
--- /dev/null
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <ranges>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+#include <ranges>
+
+#include "test_macros.h"
+
+static_assert(std::__different_from<int, char>);
+static_assert(std::__different_from<const int, char>);
+static_assert(!std::__different_from<const int, int>);
+static_assert(!std::__different_from<const volatile int, int>);
+static_assert(!std::__different_from<const int&, int>);
+static_assert(!std::__different_from<int&, int>);
+static_assert(!std::__different_from<int&&, int>);
+static_assert(!std::__different_from<int, int&>);
+static_assert(!std::__different_from<int&, const int&>);
+static_assert(!std::__different_from<int(&)(), int()>);
+static_assert(std::__different_from<int(&)(), int(*)()>);
+
diff --git a/libcxx/test/libcxx/ranges/range.utility.helpers/has_arrow.compile.pass.cpp b/libcxx/test/libcxx/ranges/range.utility.helpers/has_arrow.compile.pass.cpp
new file mode 100644 (file)
index 0000000..889cf8b
--- /dev/null
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <ranges>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+#include <ranges>
+
+#include "test_macros.h"
+
+struct simple_iter {
+    typedef std::input_iterator_tag         iterator_category;
+    typedef int                             value_type;
+    typedef std::ptrdiff_t                  difference_type;
+    typedef int*                            pointer;
+    typedef int&                            reference;
+
+    reference operator*() const;
+    pointer operator->() const;
+    friend bool operator==(const simple_iter&, const simple_iter&);
+    friend bool operator< (const simple_iter&, const simple_iter&);
+    friend bool operator<=(const simple_iter&, const simple_iter&);
+    friend bool operator> (const simple_iter&, const simple_iter&);
+    friend bool operator>=(const simple_iter&, const simple_iter&);
+
+    simple_iter& operator++();
+    simple_iter operator++(int);
+};
+
+struct no_star {
+    typedef std::input_iterator_tag         iterator_category;
+    typedef int                             value_type;
+    typedef std::ptrdiff_t                  difference_type;
+    typedef int*                            pointer;
+    typedef int&                            reference;
+
+ /* reference operator*() const; */
+    pointer operator->() const;
+    friend bool operator==(const simple_iter&, const simple_iter&);
+    friend bool operator< (const simple_iter&, const simple_iter&);
+    friend bool operator<=(const simple_iter&, const simple_iter&);
+    friend bool operator> (const simple_iter&, const simple_iter&);
+    friend bool operator>=(const simple_iter&, const simple_iter&);
+
+    simple_iter& operator++();
+    simple_iter operator++(int);
+};
+
+struct no_arrow {
+    typedef std::input_iterator_tag         iterator_category;
+    typedef int                             value_type;
+    typedef std::ptrdiff_t                  difference_type;
+    typedef int*                            pointer;
+    typedef int&                            reference;
+
+    reference operator*() const;
+ /* pointer operator->() const; */
+    friend bool operator==(const simple_iter&, const simple_iter&);
+    friend bool operator< (const simple_iter&, const simple_iter&);
+    friend bool operator<=(const simple_iter&, const simple_iter&);
+    friend bool operator> (const simple_iter&, const simple_iter&);
+    friend bool operator>=(const simple_iter&, const simple_iter&);
+
+    simple_iter& operator++();
+    simple_iter operator++(int);
+};
+
+struct E {};
+struct Incomplete;
+
+static_assert(std::__has_arrow<int*>);
+static_assert(std::__has_arrow<E*>);
+static_assert(!std::__has_arrow<Incomplete*>); // Because it's not an input_iterator.
+static_assert(std::__has_arrow<simple_iter>);
+static_assert(!std::__has_arrow<no_star>);
+static_assert(!std::__has_arrow<no_arrow>);
diff --git a/libcxx/test/libcxx/ranges/range.utility.helpers/simple_view.compile.pass.cpp b/libcxx/test/libcxx/ranges/range.utility.helpers/simple_view.compile.pass.cpp
new file mode 100644 (file)
index 0000000..c544d92
--- /dev/null
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <ranges>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+struct SimpleView : std::ranges::view_base {
+  friend int* begin(SimpleView&);
+  friend int* begin(SimpleView const&);
+  friend int* end(SimpleView&);
+  friend int* end(SimpleView const&);
+};
+
+struct WrongConstView : std::ranges::view_base {
+  friend       int* begin(WrongConstView&);
+  friend const int* begin(WrongConstView const&);
+  friend       int* end(WrongConstView&);
+  friend const int* end(WrongConstView const&);
+};
+
+struct NoConstView : std::ranges::view_base {
+  friend int* begin(NoConstView&);
+  friend int* end(NoConstView&);
+};
+
+struct DifferentSentinel : std::ranges::view_base {
+  friend int* begin(DifferentSentinel&);
+  friend int* begin(DifferentSentinel const&);
+  friend sentinel_wrapper<int*> end(DifferentSentinel&);
+  friend sentinel_wrapper<int*> end(DifferentSentinel const&);
+};
+
+static_assert( std::ranges::__simple_view<SimpleView>);
+static_assert(!std::ranges::__simple_view<WrongConstView>);
+static_assert(!std::ranges::__simple_view<NoConstView>);
+static_assert(!std::ranges::__simple_view<DifferentSentinel>);