From: Yedidya Feldblum Date: Fri, 11 Nov 2016 06:02:17 +0000 (-0800) Subject: Let folly::range(const std::array<...>&) be constexpr X-Git-Tag: v2016.11.14.00~9 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b2bd22d3a1ef264e4c4bf410f7b43c2f1f5832d0;p=folly.git Let folly::range(const std::array<...>&) be constexpr Summary: [Folly] Let `folly::range(const std::array<...>&)` be `constexpr`. This may be useful for future work. Reviewed By: simpkins Differential Revision: D4165608 fbshipit-source-id: 8f1064c3cdc07301e198acd596ff26724d603cdd --- diff --git a/folly/Range.h b/folly/Range.h index 06123d34..780e1c86 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -885,6 +885,12 @@ constexpr Range range(T (&array)[n]) { return Range(array, array + n); } +template +constexpr Range range(const std::array& array) { + using r = Range; + return array.empty() ? r{} : r(&array.at(0), &array.at(0) + n); +} + typedef Range StringPiece; typedef Range MutableStringPiece; typedef Range ByteRange; diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index 49e9a86c..c918a53c 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -1106,6 +1106,21 @@ TEST(RangeFunc, ConstexprCArray) { EXPECT_EQ(4, numArrayRangeSize); } +TEST(RangeFunc, ConstexprStdArray) { + static constexpr const std::array numArray = {3, 17, 1, 9}; + constexpr const auto numArrayRange = range(numArray); + EXPECT_EQ(17, numArrayRange[1]); + constexpr const auto numArrayRangeSize = numArrayRange.size(); + EXPECT_EQ(4, numArrayRangeSize); +} + +TEST(RangeFunc, ConstexprStdArrayZero) { + static constexpr const std::array numArray = {}; + constexpr const auto numArrayRange = range(numArray); + constexpr const auto numArrayRangeSize = numArrayRange.size(); + EXPECT_EQ(0, numArrayRangeSize); +} + TEST(RangeFunc, ConstexprIteratorPair) { static constexpr const int numArray[4] = {3, 17, 1, 9}; constexpr const auto numPtr = static_cast(numArray);