Detect and use std::is_trivially_copyable<T> where possible.
authorPeter Griess <pgriess@fb.com>
Thu, 10 Oct 2013 17:12:00 +0000 (10:12 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 24 Oct 2013 21:53:41 +0000 (14:53 -0700)
Summary:
- Sadly, boost::has_trivial_copy<T> is not a suitable replacement for
std::is_trivially_copyable<T> on libc++. Fortunately, the latter is
actually supported (and works), so use it directly.

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

Reviewed By: delong.j@fb.com

FB internal diff: D1008921

folly/Portability.h
folly/configure.ac
folly/small_vector.h
folly/test/small_vector_test.cpp
folly/test/stl_tests/OFBVector.h

index d017400e3a4d020f72e23c77817666330248bae9..47d33be4a5a690babb58f82c1b3dbcee2535a89c 100644 (file)
@@ -137,4 +137,21 @@ struct MaxAlign { char c; } __attribute__((aligned));
 #include "folly/detail/Clock.h"
 #endif
 
+// Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
+// usage of __has_trivial_copy(), so we can't use it as a
+// least-common-denominator for C++11 implementations that don't support
+// std::is_trivially_copyable<T>.
+//
+//      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
+//
+// As a result, use std::is_trivially_copyable() where it exists, and fall back
+// to Boost otherwise.
+#if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
+#include <type_traits>
+#define FOLLY_IS_TRIVIALLY_COPYABLE(T)      (std::is_trivially_copyable<T>::value)
+#else
+#include <boost/type_traits.hpp>
+#define FOLLY_IS_TRIVIALLY_COPYABLE(T)      (boost::has_trivial_copy<T>::value)
+#endif
+
 #endif // FOLLY_PORTABILITY_H_
index 4a36476379c46168a8bfc980cfd94be68add6fcd..2c0dc95062462afedff43dc50114fd4eb1e863f2 100644 (file)
@@ -118,6 +118,15 @@ AC_COMPILE_IFELSE(
   ],
   [AC_DEFINE([USE_LIBCPP], [1], [Define to 1 if we're using libc++.])])
 
+AC_COMPILE_IFELSE(
+  [AC_LANG_SOURCE[
+    #include <type_traits>
+    const bool val = std::is_trivially_copyable<bool>::value;]
+  ],
+  [AC_DEFINE([HAVE_STD__IS_TRIVIALLY_COPYABLE], [1],
+             [Define to 1 if we have a usable std::is_trivially_copyable<T>
+              implementation.])])
+
 # Check for clock_gettime(2). This is not in an AC_CHECK_FUNCS() because we
 # want to link with librt if necessary.
 AC_SEARCH_LIBS([clock_gettime], [rt],
index 373b839311f80aaecab8da24f2289427753de6b1..88c7bb59c59c18eeb67209797648d4c89f8033b9 100644 (file)
@@ -116,7 +116,7 @@ namespace detail {
    */
   template<class T>
   typename std::enable_if<
-    !boost::has_trivial_copy<T>::value
+    !FOLLY_IS_TRIVIALLY_COPYABLE(T)
   >::type
   moveToUninitialized(T* first, T* last, T* out) {
     auto const count = last - first;
@@ -138,11 +138,10 @@ namespace detail {
     }
   }
 
-  // Specialization for trivially copyable types.  (TODO: change to
-  // std::is_trivially_copyable when that works.)
+  // Specialization for trivially copyable types.
   template<class T>
   typename std::enable_if<
-    boost::has_trivial_copy<T>::value
+    FOLLY_IS_TRIVIALLY_COPYABLE(T)
   >::type
   moveToUninitialized(T* first, T* last, T* out) {
     std::memmove(out, first, (last - first) * sizeof *first);
@@ -156,7 +155,7 @@ namespace detail {
    */
   template<class T>
   typename std::enable_if<
-    !boost::has_trivial_copy<T>::value
+    !FOLLY_IS_TRIVIALLY_COPYABLE(T)
   >::type
   moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
     if (lastConstructed == realLast) {
@@ -195,7 +194,7 @@ namespace detail {
   // change to std::is_trivially_copyable when that works.)
   template<class T>
   typename std::enable_if<
-    boost::has_trivial_copy<T>::value
+    FOLLY_IS_TRIVIALLY_COPYABLE(T)
   >::type
   moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
     std::move_backward(first, lastConstructed, realLast);
index ad70e628e0e7b1738318cc883f988db29e9b0209..3df4fb971e5f77e5a402aa9841d4c1e08ab6b338 100644 (file)
@@ -84,8 +84,8 @@ struct NontrivialType {
 
   int32_t a;
 };
-static_assert(!boost::has_trivial_copy<NontrivialType>::value,
-              "NontrivialType isn't trivially copyable");
+static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(NontrivialType),
+              "NontrivialType is trivially copyable");
 
 int NontrivialType::ctored = 0;
 
@@ -148,6 +148,9 @@ struct NoncopyableCounter {
 };
 int NoncopyableCounter::alive = 0;
 
+static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(NoncopyableCounter),
+              "NoncopyableCounter is trivially copyable");
+
 // Check that throws don't break the basic guarantee for some cases.
 // Uses the method for testing exception safety described at
 // http://www.boost.org/community/exception_safety.html, to force all
@@ -472,9 +475,10 @@ TEST(small_vector, Iteration) {
 }
 
 TEST(small_vector, NonCopyableType) {
-  folly::small_vector<std::unique_ptr<std::string>,2> vec;
+  folly::small_vector<NontrivialType,2> vec;
+
   for (int i = 0; i < 10; ++i) {
-    vec.emplace(vec.begin(), new std::string("asd"));
+    vec.emplace(vec.begin(), 13);
   }
   EXPECT_EQ(vec.size(), 10);
   auto vec2 = std::move(vec);
index 88e18273c2233519a4cfcf86786193e58d6106e4..f5f11c5e5119e9553c257e31a1a227ee19e4d8ea 100644 (file)
@@ -179,7 +179,7 @@ void uninitializedFillDefaultOrFree(T * b, size_t n) {
 template <class T>
 void uninitializedFillOrFree(T * b, size_t n, const T& value) {
   auto const e = b + n;
-  if (boost::has_trivial_copy<T>::value) {
+  if (FOLLY_IS_TRIVIALLY_COPYABLE(T)) {
     auto i = b;
     auto const e1 = b + (n & ~size_t(7));
     for (; i != e1; i += 8) {
@@ -764,7 +764,7 @@ public:
     memmove(const_cast<T*>(position) + n,
             position,
             sizeof(T) * (e_ - position));
-    if (boost::has_trivial_copy<T>::value) {
+    if (FOLLY_IS_TRIVIALLY_COPYABLE(T)) {
       std::uninitialized_fill(const_cast<T*>(position),
                               const_cast<T*>(position) + n,
                               x);