[libcxx-commits] [libcxx] e5d483f - [libcxx][ranges] Add ranges::empty CPO.

via libcxx-commits libcxx-commits at lists.llvm.org
Mon May 10 17:15:59 PDT 2021


Author: zoecarver
Date: 2021-05-10T17:14:39-07:00
New Revision: e5d483f28a3af0972fc9b0df6073e4c14bb39359

URL: https://github.com/llvm/llvm-project/commit/e5d483f28a3af0972fc9b0df6073e4c14bb39359
DIFF: https://github.com/llvm/llvm-project/commit/e5d483f28a3af0972fc9b0df6073e4c14bb39359.diff

LOG: [libcxx][ranges] Add ranges::empty CPO.

Depends on D101079. Refs D101189.

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

Added: 
    libcxx/include/__ranges/empty.h
    libcxx/test/std/ranges/range.access/range.prim/empty.incomplete.verify.cpp
    libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp

Modified: 
    libcxx/include/CMakeLists.txt
    libcxx/include/ranges

Removed: 
    


################################################################################
diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 9648d980a0320..d08d156a8a0af 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -37,6 +37,7 @@ set(files
   __nullptr
   __ranges/access.h
   __ranges/concepts.h
+  __ranges/empty.h
   __ranges/enable_borrowed_range.h
   __ranges/view.h
   __ranges/size.h

diff  --git a/libcxx/include/__ranges/empty.h b/libcxx/include/__ranges/empty.h
new file mode 100644
index 0000000000000..65f36cd1c6cb4
--- /dev/null
+++ b/libcxx/include/__ranges/empty.h
@@ -0,0 +1,83 @@
+// -*- 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_EMPTY_H
+#define _LIBCPP___RANGES_EMPTY_H
+
+#include <__config>
+#include <__iterator/concepts.h>
+#include <__ranges/size.h>
+#include <type_traits>
+
+#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)
+
+// clang-format off
+namespace ranges {
+// [range.prim.empty]
+namespace __empty {
+  template <class _Tp>
+  concept __member_empty = requires(_Tp&& __t) {
+    bool(_VSTD::forward<_Tp>(__t).empty());
+  };
+
+  template<class _Tp>
+  concept __can_invoke_size =
+    !__member_empty<_Tp> &&
+    requires(_Tp&& __t) { ranges::size(_VSTD::forward<_Tp>(__t)); };
+
+  template <class _Tp>
+  concept __can_compare_begin_end =
+    !__can_invoke_size<_Tp> &&
+    requires(_Tp&& __t) {
+      bool(ranges::begin(__t) == ranges::end(__t));
+      { ranges::begin(__t) } -> forward_iterator;
+    };
+
+  struct __fn {
+    template <__member_empty _Tp>
+    [[nodiscard]] constexpr bool operator()(_Tp&& __t) const
+        noexcept(noexcept(bool(__t.empty()))) {
+      return __t.empty();
+    }
+
+    template <__can_invoke_size _Tp>
+    [[nodiscard]] constexpr bool operator()(_Tp&& __t) const
+        noexcept(noexcept(ranges::size(_VSTD::forward<_Tp>(__t)))) {
+      return ranges::size(_VSTD::forward<_Tp>(__t)) == 0;
+    }
+
+    template<__can_compare_begin_end _Tp>
+    [[nodiscard]] constexpr bool operator()(_Tp&& __t) const
+        noexcept(noexcept(bool(ranges::begin(__t) == ranges::end(__t)))) {
+      return ranges::begin(__t) == ranges::end(__t);
+    }
+  };
+}
+
+inline namespace __cpo {
+  inline constexpr auto empty = __empty::__fn{};
+} // namespace __cpo
+} // namespace ranges
+// clang-format off
+
+#endif // !defined(_LIBCPP_HAS_NO_RANGES)
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___RANGES_EMPTY_H

diff  --git a/libcxx/include/ranges b/libcxx/include/ranges
index c2b6bb306530c..5401add0eab41 100644
--- a/libcxx/include/ranges
+++ b/libcxx/include/ranges
@@ -78,6 +78,7 @@ namespace std::ranges {
 #include <__config>
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
+#include <__ranges/empty.h>
 #include <__ranges/enable_borrowed_range.h>
 #include <__ranges/view.h>
 #include <__ranges/size.h>

diff  --git a/libcxx/test/std/ranges/range.access/range.prim/empty.incomplete.verify.cpp b/libcxx/test/std/ranges/range.access/range.prim/empty.incomplete.verify.cpp
new file mode 100644
index 0000000000000..ae374176bb0b9
--- /dev/null
+++ b/libcxx/test/std/ranges/range.access/range.prim/empty.incomplete.verify.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: gcc-10
+
+// std::ranges::empty
+
+#include <ranges>
+
+struct Incomplete;
+
+void f(Incomplete arr[]) {
+  // expected-error@*:* {{is SFINAE-unfriendly on arrays of an incomplete type.}}
+  // expected-error@*:* {{call to deleted function call operator in type}}
+  // expected-error@*:* {{attempt to use a deleted function}}
+  std::ranges::begin(arr);
+}
+
+void f(Incomplete(&arr)[]) {
+  // expected-error@*:* {{is SFINAE-unfriendly on arrays of an incomplete type.}}
+  std::ranges::begin(arr);
+}
+
+void f(Incomplete(&&arr)[]) {
+  // expected-error@*:* {{is SFINAE-unfriendly on arrays of an incomplete type.}}
+  std::ranges::begin(arr);
+}
+
+void f2(Incomplete arr[2]) {
+  // expected-error@*:* {{call to deleted function call operator in type}}
+  // expected-error@*:* {{attempt to use a deleted function}}
+  std::ranges::begin(arr);
+}
+
+void f(Incomplete(&arr)[2]) {
+  std::ranges::begin(arr);
+}
+
+void f(Incomplete(&&arr)[2]) {
+  std::ranges::begin(arr);
+}
+
+void f(Incomplete(&arr)[2][2]) {
+  std::ranges::begin(arr);
+}

diff  --git a/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp b/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
new file mode 100644
index 0000000000000..6ea4b05799a52
--- /dev/null
+++ b/libcxx/test/std/ranges/range.access/range.prim/empty.pass.cpp
@@ -0,0 +1,163 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: gcc-10
+
+// std::ranges::empty
+
+#include <ranges>
+
+#include <cassert>
+#include "test_macros.h"
+#include "test_iterators.h"
+
+using RangeEmptyT = decltype(std::ranges::empty);
+using RangeSizeT = decltype(std::ranges::size);
+
+static_assert(!std::is_invocable_v<RangeEmptyT, int[]>);
+static_assert(!std::is_invocable_v<RangeEmptyT, int(&)[]>);
+static_assert(!std::is_invocable_v<RangeEmptyT, int(&&)[]>);
+static_assert( std::is_invocable_v<RangeEmptyT, int[1]>);
+static_assert( std::is_invocable_v<RangeEmptyT, const int[1]>);
+static_assert( std::is_invocable_v<RangeEmptyT, int (&&)[1]>);
+static_assert( std::is_invocable_v<RangeEmptyT, int (&)[1]>);
+static_assert( std::is_invocable_v<RangeEmptyT, const int (&)[1]>);
+
+struct NonConstSizeAndEmpty {
+  int size();
+  bool empty();
+};
+static_assert(!std::is_invocable_v<RangeSizeT, const NonConstSizeAndEmpty&>);
+static_assert(!std::is_invocable_v<RangeEmptyT, const NonConstSizeAndEmpty&>);
+
+struct HasMemberAndFunction {
+  constexpr bool empty() const { return true; }
+  // We should never do ADL lookup for std::ranges::empty.
+  friend bool empty(const HasMemberAndFunction&) { return false; }
+};
+
+struct BadReturnType {
+  BadReturnType empty() { return {}; }
+};
+static_assert(!std::is_invocable_v<RangeEmptyT, BadReturnType>);
+
+struct BoolConvertible {
+  constexpr operator bool() noexcept(false) { return true; }
+};
+struct BoolConvertibleReturnType {
+  constexpr BoolConvertible empty() noexcept { return {}; }
+};
+
+static_assert(!noexcept(std::ranges::empty(BoolConvertibleReturnType())));
+
+constexpr bool testEmptyMember() {
+  HasMemberAndFunction a;
+  assert(std::ranges::empty(a) == true);
+
+  BoolConvertibleReturnType b;
+  assert(std::ranges::empty(b) == true);
+
+  return true;
+}
+
+struct SizeMember {
+  size_t size_;
+  constexpr size_t size() const { return size_; }
+};
+
+struct SizeFunction {
+  size_t size_;
+  friend constexpr size_t size(SizeFunction sf) { return sf.size_; }
+};
+
+constexpr bool testUsingRangesSize() {
+  SizeMember a{1};
+  assert(std::ranges::empty(a) == false);
+  SizeMember b{0};
+  assert(std::ranges::empty(b) == true);
+
+  SizeFunction c{1};
+  assert(std::ranges::empty(c) == false);
+  SizeFunction d{0};
+  assert(std::ranges::empty(d) == true);
+
+  return true;
+}
+
+struct other_forward_iterator : forward_iterator<int*> { };
+
+struct sentinel {
+  constexpr bool operator==(std::input_or_output_iterator auto) const { return true; }
+};
+
+struct BeginEndNotSizedSentinel {
+  friend constexpr forward_iterator<int*> begin(BeginEndNotSizedSentinel) { return {}; }
+  friend constexpr sentinel end(BeginEndNotSizedSentinel) { return {}; }
+};
+static_assert(!std::is_invocable_v<RangeSizeT, BeginEndNotSizedSentinel&>);
+
+struct InvalidMinusBeginEnd {
+  friend constexpr random_access_iterator<int*> begin(InvalidMinusBeginEnd) { return {}; }
+  friend constexpr sentinel end(InvalidMinusBeginEnd) { return {}; }
+};
+
+// Int is integer-like, but it is not other_forward_iterator's 
diff erence_type.
+constexpr short operator-(sentinel, random_access_iterator<int*>) { return 2; }
+constexpr short operator-(random_access_iterator<int*>, sentinel) { return 2; }
+static_assert(!std::is_invocable_v<RangeSizeT, InvalidMinusBeginEnd&>);
+
+// This type will use ranges::size.
+struct IntPtrBeginAndEnd {
+  int buff[8];
+  constexpr int* begin() { return buff; }
+  constexpr int* end() { return buff + 8; }
+};
+static_assert(std::is_invocable_v<RangeSizeT, IntPtrBeginAndEnd&>);
+
+// size is disabled here, and it isn't sized_sentinel_for, so we have to compare begin
+// and end again.
+struct DisabledSizeRangeWithBeginEnd {
+  friend constexpr forward_iterator<int*> begin(DisabledSizeRangeWithBeginEnd) { return {}; }
+  friend constexpr sentinel end(DisabledSizeRangeWithBeginEnd) { return {}; }
+  constexpr size_t size() const { return 1; }
+};
+
+template<>
+inline constexpr bool std::disable_sized_range<DisabledSizeRangeWithBeginEnd> = true;
+static_assert(!std::is_invocable_v<RangeSizeT, DisabledSizeRangeWithBeginEnd&>);
+
+constexpr bool testBeginEqualsEnd() {
+  BeginEndNotSizedSentinel a;
+  assert(std::ranges::empty(a) == true);
+
+  InvalidMinusBeginEnd b;
+  assert(std::ranges::empty(b) == true);
+
+  IntPtrBeginAndEnd c;
+  assert(std::ranges::empty(c) == false);
+
+  DisabledSizeRangeWithBeginEnd d;
+  assert(std::ranges::empty(d) == true);
+
+  return true;
+}
+
+int main(int, char**) {
+  testEmptyMember();
+  static_assert(testEmptyMember());
+
+  testUsingRangesSize();
+  static_assert(testUsingRangesSize());
+
+  testBeginEqualsEnd();
+  static_assert(testBeginEqualsEnd());
+
+  return 0;
+}


        


More information about the libcxx-commits mailing list