Commit 4208cb5e authored by Brett Wilson's avatar Brett Wilson Committed by Commit Bot

Fix base::is_trivially_copyable to detect destructors.

std::is_trivially_copyable should return false for classes with nontrivial
destructors.

C++11 standard section 9.6 says a trivially copyable class is a class that:
  — has no non-trivial copy constructors (12.8),
  — has no non-trivial move constructors (12.8),
  — has no non-trivial copy assignment operators (13.5.3, 12.8),
  — has no non-trivial move assignment operators (13.5.3, 12.8), and
  — has a trivial destructor (12.4).

The current implementation for _GNUC_VER < 501 didn't detech the destructor
case. This is important because when is_trivially_copyable returns true,
container code may implement moves as memcpy, when in fact it needs to me a
memcpy + a destructor.

Bug: 
Change-Id: I5739856ca7eaf5ddcf3eeb716bdc2dc33b0de0f2
Reviewed-on: https://chromium-review.googlesource.com/575020Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Brett Wilson <brettw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487237}
parent a7580295
......@@ -87,7 +87,8 @@ struct is_trivially_copyable {
#if _GNUC_VER >= 501
static constexpr bool value = __is_trivially_copyable(T);
#else
static constexpr bool value = __has_trivial_copy(T);
static constexpr bool value =
__has_trivial_copy(T) && __has_trivial_destructor(T);
#endif
};
#else
......
......@@ -69,5 +69,26 @@ static_assert(
internal::SupportsOstreamOperator<const StructWithOperator&>::value,
"struct with operator<< should be printable by const ref");
// base::is_trivially_copyable
class TrivialCopy {
public:
TrivialCopy(int d) : data_(d) {}
protected:
int data_;
};
class TrivialCopyButWithDestructor : public TrivialCopy {
public:
TrivialCopyButWithDestructor(int d) : TrivialCopy(d) {}
~TrivialCopyButWithDestructor() { data_ = 0; }
};
static_assert(base::is_trivially_copyable<TrivialCopy>::value,
"TrivialCopy should be detected as trivially copyable");
static_assert(!base::is_trivially_copyable<TrivialCopyButWithDestructor>::value,
"TrivialCopyButWithDestructor should not be detected as "
"trivially copyable");
} // namespace
} // namespace base
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