Support constexpr StringPiece.
authorSoren Lassen <soren@fb.com>
Sun, 29 Sep 2013 00:53:52 +0000 (17:53 -0700)
committerPeter Griess <pgriess@fb.com>
Tue, 15 Oct 2013 01:45:56 +0000 (18:45 -0700)
Test Plan: fbconfig folly/test --platform=gcc-4.8.1-glibc-2.17 && fbmake dbg _bin/folly/test/range_test && _bin/folly/test/range_test

Reviewed By: tudorb@fb.com

FB internal diff: D989142

folly/Range.h
folly/test/RangeTest.cpp

index 107bdb00de461b90b00810ada687c0ac7b6c338e..cabd9078ed038d3b560f85cab6b3b838e12d6597 100644 (file)
@@ -152,8 +152,8 @@ public:
       : b_(start), e_(start + size) { }
 
   // Works only for Range<const char*>
-  /* implicit */ Range(Iter str)
-      : b_(str), e_(b_ + strlen(str)) {}
+  /* implicit */ constexpr Range(Iter str)
+      : b_(str), e_(str + strlen(str)) {}
   // Works only for Range<const char*>
   /* implicit */ Range(const std::string& str)
       : b_(str.data()), e_(b_ + str.size()) {}
index 1ed678c85bb9689aebb800e1c2eef41baf0e3d13..445df4248fb324290b594115ab4f7c962beb6302 100644 (file)
@@ -290,6 +290,16 @@ TEST(StringPiece, InvalidRange) {
   EXPECT_THROW(a.subpiece(6), std::out_of_range);
 }
 
+constexpr char helloArray[] = "hello";
+
+TEST(StringPiece, Constexpr) {
+  constexpr StringPiece hello1("hello");
+  EXPECT_EQ("hello", hello1);
+
+  constexpr StringPiece hello2(helloArray);
+  EXPECT_EQ("hello", hello2);
+}
+
 TEST(qfind, UInt32_Ranges) {
   vector<uint32_t> a({1, 2, 3, 260, 5});
   vector<uint32_t> b({2, 3, 4});