[PATCH] D49402: [STLExtras] Add size() for arrays

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 27 17:46:57 PDT 2018


Neat! That seems to work:

```
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 94365dd9ced..47450623826 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1044,11 +1044,11 @@ void erase_if(Container &C, UnaryPredicate P) {
 template <typename R>
 auto size(R &&Range, typename std::enable_if<
                          std::is_same<typename std::iterator_traits<decltype(
-                                          Range.begin())>::iterator_category,
+                                          adl_begin(Range))>::iterator_category,
                                       std::random_access_iterator_tag>::value,
                          void>::type * = nullptr)
-    -> decltype(std::distance(Range.begin(), Range.end())) {
-  return std::distance(Range.begin(), Range.end());
+    -> decltype(std::distance(adl_begin(Range), adl_end(Range))) {
+  return std::distance(adl_begin(Range), adl_end(Range));
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/llvm/unittests/ADT/IteratorTest.cpp b/llvm/unittests/ADT/IteratorTest.cpp
index 50c3b01bbc7..b335bdec766 100644
--- a/llvm/unittests/ADT/IteratorTest.cpp
+++ b/llvm/unittests/ADT/IteratorTest.cpp
@@ -382,9 +382,13 @@ TEST(ZipIteratorTest, Reverse) {
 TEST(RangeTest, Distance) {
   std::vector<int> v1;
   std::vector<int> v2{1, 2, 3};
+  int a1[] = {1, 2, 3};
+  int a2[5] = {0};
 
   EXPECT_EQ(std::distance(v1.begin(), v1.end()), size(v1));
   EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
+  EXPECT_EQ(array_lengthof(a1), size(a1));
+  EXPECT_EQ(array_lengthof(a2), size(a2));
 }
 
 TEST(IteratorRangeTest, DropBegin) {
```

Do folks think this is worth supporting?

vedant

> On Jul 23, 2018, at 4:23 PM, David Blaikie <dblaikie at gmail.com> wrote:
> 
> Seems like the existing size(R &&Range) function could be generalized (by using adl_begin/end rather than member begin/end? Would that be enough) to cover arrays as well, maybe?
> 
> On Wed, Jul 18, 2018 at 11:24 AM Paul Semel via Phabricator via llvm-commits <llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>> wrote:
> paulsemel closed this revision.
> paulsemel added a comment.
> 
> I agree with James !
> 
> 
> Repository:
>   rL LLVM
> 
> https://reviews.llvm.org/D49402 <https://reviews.llvm.org/D49402>
> 
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180727/1208dd7b/attachment-0001.html>


More information about the llvm-commits mailing list