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
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 {
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) {