Assorted tweaks to folly/String.h
authorYedidya Feldblum <yfeldblum@fb.com>
Tue, 12 Dec 2017 04:19:06 +0000 (20:19 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 12 Dec 2017 04:22:11 +0000 (20:22 -0800)
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
folly/String.cpp
folly/String.h

index f6b5ba993dffbba4d2cc318db4c269f314a0833d..597897db3f2c26b8c8708cbc80b1ea03639c0de9 100644 (file)
@@ -401,7 +401,8 @@ void splitTo(const Delim& delimiter,
 
 template <bool exact, class Delim, class... OutputTypes>
 typename std::enable_if<
-    AllConvertible<OutputTypes...>::value && sizeof...(OutputTypes) >= 1,
+    StrictConjunction<IsConvertible<OutputTypes>...>::value &&
+        sizeof...(OutputTypes) >= 1,
     bool>::type
 split(const Delim& delimiter, StringPiece input, OutputTypes&... outputs) {
   return detail::splitFixed<exact>(
index 097e5bbfd4c7b2cd86ed2b388ee557bea5124952..f4a923963f3ecd05fabb76790fc949b51361ac7c 100644 (file)
 
 namespace folly {
 
+static_assert(IsConvertible<float>::value, "");
+static_assert(IsConvertible<int>::value, "");
+static_assert(IsConvertible<bool>::value, "");
+static_assert(IsConvertible<int>::value, "");
+static_assert(!IsConvertible<std::vector<int>>::value, "");
+
 static inline bool is_oddspace(char c) {
   return c == '\n' || c == '\t' || c == '\r';
 }
index 750863b986bbe52b4812aaa0ed8f84cc89052779..0251b987be1287a8d0137cd3aa1bc8a3fb5c42ca 100644 (file)
@@ -34,6 +34,7 @@
 #include <folly/Portability.h>
 #include <folly/Range.h>
 #include <folly/ScopeGuard.h>
+#include <folly/Traits.h>
 
 // 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 <class T, class Enable = void>
-struct IsConvertible {
-  enum { value = false };
-};
+namespace detail {
+template <typename Void, typename OutputType>
+struct IsConvertible : std::false_type {};
 
-template <class T>
+template <typename OutputType>
 struct IsConvertible<
-    T,
-    decltype(static_cast<void>(
-        parseTo(std::declval<folly::StringPiece>(), std::declval<T&>())))> {
-  enum { value = true };
-};
-
-template <class... Types>
-struct AllConvertible;
-
-template <class Head, class... Tail>
-struct AllConvertible<Head, Tail...> {
-  enum { value = IsConvertible<Head>::value && AllConvertible<Tail...>::value };
-};
-
-template <>
-struct AllConvertible<> {
-  enum { value = true };
-};
-
-static_assert(AllConvertible<float>::value, "");
-static_assert(AllConvertible<int>::value, "");
-static_assert(AllConvertible<bool>::value, "");
-static_assert(AllConvertible<int>::value, "");
-static_assert(!AllConvertible<std::vector<int>>::value, "");
+    void_t<decltype(parseTo(StringPiece{}, std::declval<OutputType&>()))>,
+    OutputType> : std::true_type {};
+} // namespace detail
+template <typename OutputType>
+struct IsConvertible : detail::IsConvertible<void, OutputType> {};
 
 template <bool exact = true, class Delim, class... OutputTypes>
 typename std::enable_if<
-    AllConvertible<OutputTypes...>::value && sizeof...(OutputTypes) >= 1,
+    StrictConjunction<IsConvertible<OutputTypes>...>::value &&
+        sizeof...(OutputTypes) >= 1,
     bool>::type
 split(const Delim& delimiter, StringPiece input, OutputTypes&... outputs);