Commit efc4ca1e authored by Phil Willoughby's avatar Phil Willoughby Committed by Facebook Github Bot

Fix the copy constructor in Replaceable

Summary:
Fix the copy constructor, and add the missing testcase which would have found
this problem.

Reviewed By: yfeldblum

Differential Revision: D5516628

fbshipit-source-id: 3e688c34f061511df5b68243111fecb83483d79d
parent 890625b2
...@@ -307,7 +307,8 @@ struct copy_ctor_mixin<T, true> { ...@@ -307,7 +307,8 @@ struct copy_ctor_mixin<T, true> {
copy_ctor_mixin() = default; copy_ctor_mixin() = default;
inline copy_ctor_mixin(copy_ctor_mixin const& other) noexcept( inline copy_ctor_mixin(copy_ctor_mixin const& other) noexcept(
std::is_nothrow_constructible<T, T const&>::value) { std::is_nothrow_constructible<T, T const&>::value) {
::new (reinterpret_cast<Replaceable<T>*>(this)->storage_) T(*other); ::new (reinterpret_cast<Replaceable<T>*>(this)->storage_)
T(*reinterpret_cast<Replaceable<T> const&>(other));
} }
copy_ctor_mixin(copy_ctor_mixin&&) = default; copy_ctor_mixin(copy_ctor_mixin&&) = default;
copy_ctor_mixin& operator=(copy_ctor_mixin&&) = default; copy_ctor_mixin& operator=(copy_ctor_mixin&&) = default;
......
...@@ -22,6 +22,7 @@ using namespace ::testing; ...@@ -22,6 +22,7 @@ using namespace ::testing;
using namespace ::folly; using namespace ::folly;
namespace { namespace {
struct Basic {};
struct alignas(128) BigAlign {}; struct alignas(128) BigAlign {};
struct HasConst final { struct HasConst final {
bool const b1; bool const b1;
...@@ -81,6 +82,7 @@ using StaticAttributeTypes = ::testing::Types< ...@@ -81,6 +82,7 @@ using StaticAttributeTypes = ::testing::Types<
float, float,
double, double,
char[11], char[11],
Basic,
BigAlign, BigAlign,
HasConst, HasConst,
HasRef, HasRef,
...@@ -250,6 +252,18 @@ TEST(ReplaceableTest, Basics) { ...@@ -250,6 +252,18 @@ TEST(ReplaceableTest, Basics) {
EXPECT_TRUE(rHasConstB->b1); EXPECT_TRUE(rHasConstB->b1);
} }
TEST(ReplaceableTest, Constructors) {
Basic b{};
// From existing `T`
auto rBasicCopy1 = Replaceable<Basic>(b);
auto rBasicMove1 = Replaceable<Basic>(std::move(b));
// From existing `Replaceable<T>`
auto rBasicCopy2 = Replaceable<Basic>(rBasicCopy1);
auto rBasicMove2 = Replaceable<Basic>(std::move(rBasicMove1));
(void)rBasicCopy2;
(void)rBasicMove2;
}
TEST(ReplaceableTest, DestructsWhenExpected) { TEST(ReplaceableTest, DestructsWhenExpected) {
int i{0}; int i{0};
{ {
......
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