From: Peter Griess Date: Wed, 9 Oct 2013 06:01:34 +0000 (-0700) Subject: Detect C++ implementations that support constexpr strlen() X-Git-Tag: v0.22.0~824 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e5b563e9dad4c29ce666653da8a294708ef06a0e;p=folly.git Detect C++ implementations that support constexpr strlen() 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 --- diff --git a/folly/Range.h b/folly/Range.h index 6925c6d4..2041d29b 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -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 /* implicit */ constexpr Range(Iter str) : b_(str), e_(str + strlen(str)) {} +#else + // Works only for Range + /* implicit */ Range(Iter str) + : b_(str), e_(str + strlen(str)) {} +#endif // Works only for Range /* implicit */ Range(const std::string& str) : b_(str.data()), e_(b_ + str.size()) {} diff --git a/folly/configure.ac b/folly/configure.ac index 1b5e7b10..b3e89bb1 100644 --- a/folly/configure.ac +++ b/folly/configure.ac @@ -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 + 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 diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index a6fe97e9..79698893 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -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 a({1, 2, 3, 260, 5});