From: Eric Niebler Date: Wed, 30 Nov 2016 03:42:04 +0000 (-0800) Subject: Add constexpr to the simple folly::Range accessors (begin, end, data, empty, etc) X-Git-Tag: v2016.12.05.00~11 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=f972b1ee32509823b2331c9bdecac14949e7e740;p=folly.git Add constexpr to the simple folly::Range accessors (begin, end, data, empty, etc) Summary: More constexpr is better! Reviewed By: yfeldblum, luciang, ot Differential Revision: D4244996 fbshipit-source-id: 30a9b726c115a92bb18538d7f18e50eccb0a1ef6 --- diff --git a/folly/Range.h b/folly/Range.h index 780e1c86..b715e1d0 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -363,16 +363,30 @@ public: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448 return e_ - b_; } - size_type walk_size() const { + constexpr size_type walk_size() const { return std::distance(b_, e_); } - bool empty() const { return b_ == e_; } - Iter data() const { return b_; } - Iter start() const { return b_; } - Iter begin() const { return b_; } - Iter end() const { return e_; } - Iter cbegin() const { return b_; } - Iter cend() const { return e_; } + constexpr bool empty() const { + return b_ == e_; + } + constexpr Iter data() const { + return b_; + } + constexpr Iter start() const { + return b_; + } + constexpr Iter begin() const { + return b_; + } + constexpr Iter end() const { + return e_; + } + constexpr Iter cbegin() const { + return b_; + } + constexpr Iter cend() const { + return e_; + } value_type& front() { assert(b_ < e_); return *b_; diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index cf577f9f..e1130e5b 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -1297,3 +1297,15 @@ TEST(Range, Constructors) { EXPECT_EQ(subpiece1.begin(), subpiece2.begin()); EXPECT_EQ(subpiece1.end(), subpiece2.end()); } + +TEST(Range, ConstexprAccessors) { + constexpr StringPiece piece = range("hello"); + static_assert(piece.size() == 6u, ""); + static_assert(piece.end() - piece.begin() == 6u, ""); + static_assert(piece.data() == piece.begin(), ""); + static_assert(piece.start() == piece.begin(), ""); + static_assert(piece.cbegin() == piece.begin(), ""); + static_assert(piece.cend() == piece.end(), ""); + static_assert(*piece.begin() == 'h', ""); + static_assert(*(piece.end() - 1) == '\0', ""); +}