Commit 8cb615a2 authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot 6

make Range::size() constexpr

Summary:
Declare size() to return a constexpr value, so it can be called on constexpr
Range objects.

This unfortunately does drop the existing assert() check, due to a gcc bug:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448

Reviewed By: lbrandy

Differential Revision: D3394612

fbshipit-source-id: 77ea3b961dc323a39dea6f0e5850f9a311210d09
parent 67a55881
......@@ -353,8 +353,13 @@ public:
reset(str.data(), str.size());
}
size_type size() const {
assert(b_ <= e_);
constexpr size_type size() const {
// It would be nice to assert(b_ <= e_) here. This can be achieved even
// in a C++11 compatible constexpr function:
// http://ericniebler.com/2014/09/27/assert-and-constexpr-in-cxx11/
// Unfortunately current gcc versions have a bug causing it to reject
// this check in a constexpr function:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448
return e_ - b_;
}
size_type walk_size() const {
......
......@@ -298,9 +298,11 @@ constexpr char helloArray[] = "hello";
TEST(StringPiece, Constexpr) {
constexpr StringPiece hello1("hello");
EXPECT_EQ("hello", hello1);
static_assert(hello1.size() == 5, "hello size should be 5 at compile time");
constexpr StringPiece hello2(helloArray);
EXPECT_EQ("hello", hello2);
static_assert(hello2.size() == 5, "hello size should be 5 at compile time");
}
TEST(StringPiece, Prefix) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment