Commit e5b563e9 authored by Peter Griess's avatar Peter Griess Committed by Sara Golemon

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
parent 31005a98
......@@ -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()) {}
......
......@@ -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>
......
......@@ -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});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment