[libcxx-commits] [libcxx] 182ba8a - [libcxx][ranges] makes `ranges::subrange` a borrowed range

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 17 10:26:25 PDT 2021


Author: Christopher Di Bella
Date: 2021-07-17T17:25:56Z
New Revision: 182ba8ab1b70a1604d4d29c214261456b589e730

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

LOG: [libcxx][ranges] makes `ranges::subrange` a borrowed range

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

Added: 
    libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp

Modified: 
    libcxx/include/__ranges/subrange.h
    libcxx/include/ranges

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__ranges/subrange.h b/libcxx/include/__ranges/subrange.h
index 8ce20bd180bf3..7001c52073b69 100644
--- a/libcxx/include/__ranges/subrange.h
+++ b/libcxx/include/__ranges/subrange.h
@@ -226,6 +226,9 @@ namespace ranges {
     else
       return __subrange.end();
   }
+
+  template<class _Ip, class _Sp, subrange_kind _Kp>
+  inline constexpr bool enable_borrowed_range<subrange<_Ip, _Sp, _Kp>> = true;
 } // namespace ranges
 
 using ranges::get;

diff  --git a/libcxx/include/ranges b/libcxx/include/ranges
index d662e2604e988..367ad60226d4e 100644
--- a/libcxx/include/ranges
+++ b/libcxx/include/ranges
@@ -93,6 +93,16 @@ namespace std::ranges {
     requires is_class_v<D> && same_as<D, remove_cv_t<D>>
   class view_interface;
 
+  // [range.subrange], sub-ranges
+  enum class subrange_kind : bool { unsized, sized };
+
+  template<input_or_output_iterator I, sentinel_for<I> S = I, subrange_kind K = see below>
+    requires (K == subrange_kind::sized || !sized_sentinel_for<S, I>)
+  class subrange;
+
+  template<class I, class S, subrange_kind K>
+    inline constexpr bool enable_borrowed_range<subrange<I, S, K>> = true;
+
   // [range.empty], empty view
   template<class T>
     requires is_object_v<T>

diff  --git a/libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp b/libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp
new file mode 100644
index 0000000000000..1c7f6e4cf2f27
--- /dev/null
+++ b/libcxx/test/std/ranges/range.utility/range.subrange/enable_borrowed_range.compile.pass.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// class std::ranges::subrange;
+
+#include <ranges>
+
+#include "test_iterators.h"
+
+namespace ranges = std::ranges;
+
+static_assert(ranges::borrowed_range<ranges::subrange<int*>>);
+static_assert(ranges::borrowed_range<ranges::subrange<int*, int const*>>);
+static_assert(ranges::borrowed_range<ranges::subrange<int*, sentinel_wrapper<int*>, ranges::subrange_kind::unsized>>);


        


More information about the libcxx-commits mailing list