[PATCH] D146231: [ADT] Add `llvm::range_size` function for generic ranges

David Blaikie via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 16 12:37:21 PDT 2023


dblaikie added inline comments.


================
Comment at: llvm/include/llvm/ADT/STLExtras.h:1770-1784
+namespace detail {
+template <typename Range>
+using check_has_member_size = decltype(std::declval<Range &>().size());
+
+template <typename Range>
+static constexpr bool HasMemberSize =
+    is_detected<check_has_member_size, Range>::value;
----------------
Rather than detecting both member and non-member, could we standardize on non-member, like adl_begin/adl_end works? Could expose adl_size to match adl_begin/end too, in addition to this wrapper that has the fallback to distance.


================
Comment at: llvm/include/llvm/ADT/STLExtras.h:1793-1794
+template <typename R> constexpr size_t range_size(R &&Range) {
+  if constexpr (std::is_array_v<R>)
+    return std::extent<R>::value;
+  else if constexpr (detail::HasMemberSize<R>)
----------------
I'd guess this can be trivially optimized from the generic std::distance call? Maybe not worth the special case?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146231/new/

https://reviews.llvm.org/D146231



More information about the llvm-commits mailing list