From efc4ca1ecfa090028b6260f7d77373ebd0aa8b45 Mon Sep 17 00:00:00 2001 From: Phil Willoughby Date: Sat, 29 Jul 2017 00:08:27 -0700 Subject: [PATCH] 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 --- folly/Replaceable.h | 3 ++- folly/test/ReplaceableTest.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/folly/Replaceable.h b/folly/Replaceable.h index 3021ed24..bdd5519e 100644 --- a/folly/Replaceable.h +++ b/folly/Replaceable.h @@ -307,7 +307,8 @@ struct copy_ctor_mixin { copy_ctor_mixin() = default; inline copy_ctor_mixin(copy_ctor_mixin const& other) noexcept( std::is_nothrow_constructible::value) { - ::new (reinterpret_cast*>(this)->storage_) T(*other); + ::new (reinterpret_cast*>(this)->storage_) + T(*reinterpret_cast const&>(other)); } copy_ctor_mixin(copy_ctor_mixin&&) = default; copy_ctor_mixin& operator=(copy_ctor_mixin&&) = default; diff --git a/folly/test/ReplaceableTest.cpp b/folly/test/ReplaceableTest.cpp index 00eb01d9..6ab88509 100644 --- a/folly/test/ReplaceableTest.cpp +++ b/folly/test/ReplaceableTest.cpp @@ -22,6 +22,7 @@ using namespace ::testing; using namespace ::folly; namespace { +struct Basic {}; struct alignas(128) BigAlign {}; struct HasConst final { bool const b1; @@ -81,6 +82,7 @@ using StaticAttributeTypes = ::testing::Types< float, double, char[11], + Basic, BigAlign, HasConst, HasRef, @@ -250,6 +252,18 @@ TEST(ReplaceableTest, Basics) { EXPECT_TRUE(rHasConstB->b1); } +TEST(ReplaceableTest, Constructors) { + Basic b{}; + // From existing `T` + auto rBasicCopy1 = Replaceable(b); + auto rBasicMove1 = Replaceable(std::move(b)); + // From existing `Replaceable` + auto rBasicCopy2 = Replaceable(rBasicCopy1); + auto rBasicMove2 = Replaceable(std::move(rBasicMove1)); + (void)rBasicCopy2; + (void)rBasicMove2; +} + TEST(ReplaceableTest, DestructsWhenExpected) { int i{0}; { -- 2.34.1