From c01c18b26eded07d11079a77d3df36a57fd4bf16 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Mon, 11 Dec 2017 20:19:06 -0800 Subject: [PATCH] Assorted tweaks to folly/String.h Summary: [Folly] Assorted tweaks to `folly/String.h`. * Use `void_t` in the definition of `IsConvertible`. * Remove `AllConvertible`. Callers can use `StrictConjunction` instead. * Move the `static_assert`s from the header to the source file. Reviewed By: andrewjcg, ot Differential Revision: D6501515 fbshipit-source-id: 472ecf23bf7f06be211480b0aceea95f7e60dc21 --- folly/String-inl.h | 3 ++- folly/String.cpp | 6 ++++++ folly/String.h | 42 ++++++++++++------------------------------ 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/folly/String-inl.h b/folly/String-inl.h index f6b5ba99..597897db 100644 --- a/folly/String-inl.h +++ b/folly/String-inl.h @@ -401,7 +401,8 @@ void splitTo(const Delim& delimiter, template typename std::enable_if< - AllConvertible::value && sizeof...(OutputTypes) >= 1, + StrictConjunction...>::value && + sizeof...(OutputTypes) >= 1, bool>::type split(const Delim& delimiter, StringPiece input, OutputTypes&... outputs) { return detail::splitFixed( diff --git a/folly/String.cpp b/folly/String.cpp index 097e5bbf..f4a92396 100644 --- a/folly/String.cpp +++ b/folly/String.cpp @@ -29,6 +29,12 @@ namespace folly { +static_assert(IsConvertible::value, ""); +static_assert(IsConvertible::value, ""); +static_assert(IsConvertible::value, ""); +static_assert(IsConvertible::value, ""); +static_assert(!IsConvertible>::value, ""); + static inline bool is_oddspace(char c) { return c == '\n' || c == '\t' || c == '\r'; } diff --git a/folly/String.h b/folly/String.h index 750863b9..0251b987 100644 --- a/folly/String.h +++ b/folly/String.h @@ -34,6 +34,7 @@ #include #include #include +#include // Compatibility function, to make sure toStdString(s) can be called // to convert a std::string or fbstring variable s into type std::string @@ -468,41 +469,22 @@ void splitTo(const Delim& delimiter, * Note that this will likely not work if the last field's target is of numeric * type, in which case folly::to<> will throw an exception. */ -template -struct IsConvertible { - enum { value = false }; -}; +namespace detail { +template +struct IsConvertible : std::false_type {}; -template +template struct IsConvertible< - T, - decltype(static_cast( - parseTo(std::declval(), std::declval())))> { - enum { value = true }; -}; - -template -struct AllConvertible; - -template -struct AllConvertible { - enum { value = IsConvertible::value && AllConvertible::value }; -}; - -template <> -struct AllConvertible<> { - enum { value = true }; -}; - -static_assert(AllConvertible::value, ""); -static_assert(AllConvertible::value, ""); -static_assert(AllConvertible::value, ""); -static_assert(AllConvertible::value, ""); -static_assert(!AllConvertible>::value, ""); + void_t()))>, + OutputType> : std::true_type {}; +} // namespace detail +template +struct IsConvertible : detail::IsConvertible {}; template typename std::enable_if< - AllConvertible::value && sizeof...(OutputTypes) >= 1, + StrictConjunction...>::value && + sizeof...(OutputTypes) >= 1, bool>::type split(const Delim& delimiter, StringPiece input, OutputTypes&... outputs); -- 2.34.1