[libcxx-commits] [PATCH] D139791: [libc++]Implement LWG-3646 std::ranges::view_interface::size returns a signed type

Igor Zhukov via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Dec 11 09:27:54 PST 2022


fsb4000 created this revision.
fsb4000 added a project: libc++.
Herald added a project: All.
fsb4000 requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139791

Files:
  libcxx/docs/Status/Cxx2bIssues.csv
  libcxx/include/__ranges/view_interface.h
  libcxx/test/std/ranges/range.utility/view.interface/view.interface.pass.cpp


Index: libcxx/test/std/ranges/range.utility/view.interface/view.interface.pass.cpp
===================================================================
--- libcxx/test/std/ranges/range.utility/view.interface/view.interface.pass.cpp
+++ libcxx/test/std/ranges/range.utility/view.interface/view.interface.pass.cpp
@@ -233,12 +233,18 @@
   static_assert(!SizeInvocable<NotSizedSentinel>);
   static_assert( SizeInvocable<ForwardRange>);
 
+  using SignedSize = std::common_type_t<std::ptrdiff_t, std::make_signed_t<std::size_t>>;
   ForwardRange forwardRange;
   assert(forwardRange.size() == 8);
   assert(static_cast<ForwardRange const&>(forwardRange).size() == 8);
 
   assert(std::ranges::size(forwardRange) == 8);
+  static_assert(std::same_as<decltype(std::ranges::size(std::declval<ForwardRange>())), std::size_t>);
+  static_assert(std::same_as<decltype(std::ranges::ssize(std::declval<ForwardRange>())), SignedSize>);
+
   assert(std::ranges::size(static_cast<ForwardRange const&>(forwardRange)) == 8);
+  static_assert(std::same_as<decltype(std::ranges::size(std::declval<ForwardRange const>())), std::size_t>);
+  static_assert(std::same_as<decltype(std::ranges::ssize(std::declval<ForwardRange const>())), SignedSize>);
 
   SizeIsTen sizeTen;
   assert(sizeTen.size() == 10);
Index: libcxx/include/__ranges/view_interface.h
===================================================================
--- libcxx/include/__ranges/view_interface.h
+++ libcxx/include/__ranges/view_interface.h
@@ -100,7 +100,7 @@
   constexpr auto size()
     requires forward_range<_D2> && sized_sentinel_for<sentinel_t<_D2>, iterator_t<_D2>>
   {
-    return ranges::end(__derived()) - ranges::begin(__derived());
+    return std::__to_unsigned_like(ranges::end(__derived()) - ranges::begin(__derived()));
   }
 
   template<class _D2 = _Derived>
@@ -108,7 +108,7 @@
   constexpr auto size() const
     requires forward_range<const _D2> && sized_sentinel_for<sentinel_t<const _D2>, iterator_t<const _D2>>
   {
-    return ranges::end(__derived()) - ranges::begin(__derived());
+    return std::__to_unsigned_like(ranges::end(__derived()) - ranges::begin(__derived()));
   }
 
   template<class _D2 = _Derived>
Index: libcxx/docs/Status/Cxx2bIssues.csv
===================================================================
--- libcxx/docs/Status/Cxx2bIssues.csv
+++ libcxx/docs/Status/Cxx2bIssues.csv
@@ -186,6 +186,8 @@
 "`3721 <https://wg21.link/LWG3721>`__","Allow an ``arg-id`` with a value of zero for ``width`` in ``std-format-spec``","July 2022","|Complete|","16.0","|format|"
 "`3724 <https://wg21.link/LWG3724>`__","``decay-copy`` should be constrained","July 2022","|Complete|","14.0"
 "","","","",""
+"`3646 <https://wg21.link/LWG3646>`__","``std::ranges::view_interface::size`` returns a signed type","November 2022","|Complete|","16.0"
+"","","","",""
 "`3629 <https://wg21.link/LWG3629>`__","``make_error_code`` and ``make_error_condition`` are customization points","Not voted in","|Complete|","16.0",""
 "`3631 <https://wg21.link/LWG3631>`__","``basic_format_arg(T&&)`` should use ``remove_cvref_t<T>`` throughout","Not voted in","|Complete|","15.0",""
 "`3645 <https://wg21.link/LWG3645>`__","``resize_and_overwrite`` is overspecified to call its callback with lvalues","Not voted in","|Complete|","14.0",""


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139791.481921.patch
Type: text/x-patch
Size: 3305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20221211/a8268f25/attachment.bin>


More information about the libcxx-commits mailing list