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
#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_
],
[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],
*/
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;
}
}
- // 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);
*/
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) {
// 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);
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;
};
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
}
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);
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) {
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);