From: Adam Simpkins Date: Wed, 8 Jun 2016 23:15:18 +0000 (-0700) Subject: make Range::size() constexpr X-Git-Tag: 2016.07.26~157 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=8cb615a27594078056b2e4ec2350660e594f5a89;hp=67a55881d21e8359640c86a0486de2f0da44390f 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 --- diff --git a/folly/Range.h b/folly/Range.h index db2de699..4a064916 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -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 { diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index 8c98ee48..294659c9 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -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) {