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 +constexpr size_t size(const T (&array)[N]) noexcept { + return N; +} + //===----------------------------------------------------------------------===// // Extra additions to //===----------------------------------------------------------------------===// 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