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

Paul Semel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 17 03:23:03 PDT 2018


paulsemel updated this revision to Diff 155837.
paulsemel marked 3 inline comments as done.
paulsemel added a comment.

Added Jame's suggestions


Repository:
  rL LLVM

https://reviews.llvm.org/D49402

Files:
  include/llvm/ADT/STLExtras.h
  unittests/ADT/STLExtrasTest.cpp


Index: unittests/ADT/STLExtrasTest.cpp
===================================================================
--- unittests/ADT/STLExtrasTest.cpp
+++ unittests/ADT/STLExtrasTest.cpp
@@ -364,4 +364,12 @@
   EXPECT_EQ(5, count);
 }
 
+TEST(STLExtrasTest, ArraySize) {
+  int a1[] = {1, 2, 3, 4};
+  char a2[] = {'H', 'i'};
+
+  EXPECT_EQ(llvm::size(a1), 4);
+  EXPECT_EQ(llvm::size(a2), 2);
+}
+
 } // namespace
Index: include/llvm/ADT/STLExtras.h
===================================================================
--- include/llvm/ADT/STLExtras.h
+++ include/llvm/ADT/STLExtras.h
@@ -1038,6 +1038,12 @@
   return std::distance(Range.begin(), Range.end());
 }
 
+/// Get the size of an array.
+template <class T, size_t N>
+constexpr size_t size(const T (&array)[N]) noexcept {
+  return N;
+}
+
 //===----------------------------------------------------------------------===//
 //     Extra additions to <memory>
 //===----------------------------------------------------------------------===//


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49402.155837.patch
Type: text/x-patch
Size: 992 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180717/78fc7288/attachment.bin>


More information about the llvm-commits mailing list