Detect C++ implementations that support constexpr strlen()
authorPeter Griess <pgriess@fb.com>
Wed, 9 Oct 2013 06:01:34 +0000 (23:01 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 24 Oct 2013 21:53:41 +0000 (14:53 -0700)
Summary:
- Only declare the Range constructor as a constexpr if strlen() is also
a constexpr. Otherwise, we'll get compilation errors.

Test Plan:
- fbconfig -r folly && fbmake runtests
- ./configure && make check on Ubuntu/FC/Mac

Reviewed By: soren@fb.com

FB internal diff: D1003124

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

index 6925c6d44f1f4be6a5b7ba54f2025fe7990e684f..2041d29b38134422afdcfacc2c25cefef4636dbc 100644 (file)
@@ -151,9 +151,15 @@ public:
   Range(Iter start, size_t size)
       : b_(start), e_(start + size) { }
 
+#if FOLLY_HAVE_CONSTEXPR_STRLEN
   // Works only for Range<const char*>
   /* implicit */ constexpr Range(Iter str)
       : b_(str), e_(str + strlen(str)) {}
+#else
+  // Works only for Range<const char*>
+  /* implicit */ Range(Iter str)
+      : b_(str), e_(str + strlen(str)) {}
+#endif
   // Works only for Range<const char*>
   /* implicit */ Range(const std::string& str)
       : b_(str.data()), e_(b_ + str.size()) {}
index 1b5e7b10fb84e16885fd32b7a90597985b78fe5c..b3e89bb13b9ae0751c408898466843e55d8bdc8d 100644 (file)
@@ -87,6 +87,13 @@ AC_COMPILE_IFELSE(
   [AC_DEFINE([HAVE_STD__THIS_THREAD__SLEEP_FOR], [1],
              [Define to 1 if std::this_thread::sleep_for() is defined.])])
 
+AC_COMPILE_IFELSE(
+  [AC_LANG_SOURCE[
+    #include <cstring>
+    static constexpr int val = strlen("foo");]],
+  [AC_DEFINE([HAVE_CONSTEXPR_STRLEN], [1],
+             [Define to 1 if strlen(3) is constexpr.])])
+
 AC_COMPILE_IFELSE(
   [AC_LANG_SOURCE[
     #include <type_traits>
index a6fe97e90da2182ec54329d4c9f01faa34802b48..79698893e7b3bcbea8ae36cc90f0456500c8ff03 100644 (file)
@@ -290,6 +290,7 @@ TEST(StringPiece, InvalidRange) {
   EXPECT_THROW(a.subpiece(6), std::out_of_range);
 }
 
+#if FOLLY_HAVE_CONSTEXPR_STRLEN
 constexpr char helloArray[] = "hello";
 
 TEST(StringPiece, Constexpr) {
@@ -299,6 +300,7 @@ TEST(StringPiece, Constexpr) {
   constexpr StringPiece hello2(helloArray);
   EXPECT_EQ("hello", hello2);
 }
+#endif
 
 TEST(qfind, UInt32_Ranges) {
   vector<uint32_t> a({1, 2, 3, 260, 5});