Skip to content

Commit

Permalink
Merge pull request #97 from jiixyj/set-engaged-flag-on-assignment
Browse files Browse the repository at this point in the history
Set 'engaged' flag on assignment correctly
  • Loading branch information
neatudarius authored Jan 2, 2025
2 parents f4cd4b7 + 9a60702 commit 08c8477
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
13 changes: 5 additions & 8 deletions include/beman/optional26/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,7 @@ inline constexpr optional<T>::optional(const optional& rhs)
requires std::is_copy_constructible_v<T> && (!std::is_trivially_copy_constructible_v<T>)
{
if (rhs.has_value()) {
std::construct_at(std::addressof(value_), rhs.value_);
engaged_ = true;
construct(rhs.value_);
}
}

Expand All @@ -434,8 +433,7 @@ inline constexpr optional<T>::optional(optional&& rhs) noexcept(std::is_nothrow_
requires std::is_move_constructible_v<T> && (!std::is_trivially_move_constructible_v<T>)
{
if (rhs.has_value()) {
std::construct_at(std::addressof(value_), std::move(rhs.value()));
engaged_ = true;
construct(std::move(rhs.value_));
}
}

Expand Down Expand Up @@ -466,8 +464,7 @@ inline constexpr optional<T>::optional(const optional<U>& rhs)
requires detail::enable_from_other<T, U, const U&> && std::is_convertible_v<const U&, T>
{
if (rhs.has_value()) {
std::construct_at(std::addressof(value_), rhs.value());
engaged_ = true;
construct(*rhs);
}
}

Expand Down Expand Up @@ -524,7 +521,7 @@ inline constexpr optional<T>& optional<T>::operator=(const optional<T>& rhs)
else if (has_value())
value_ = rhs.value_;
else
std::construct_at(std::addressof(value_), rhs.value_);
construct(rhs.value_);
return *this;
}

Expand All @@ -539,7 +536,7 @@ optional<T>::operator=(optional<T>&& rhs) noexcept(std::is_nothrow_move_construc
else if (has_value())
value_ = std::move(rhs.value_);
else
std::construct_at(std::addressof(value_), std::move(rhs.value_));
construct(std::move(rhs.value_));
return *this;
}

Expand Down
32 changes: 31 additions & 1 deletion src/beman/optional26/tests/optional.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,38 @@ TEST(OptionalTest, AssignmentValue) {
*/
beman::optional26::optional<not_trivial_copy_assignable> o5{5};
beman::optional26::optional<not_trivial_copy_assignable> o6;

// Copy into empty optional.
o6 = o5;
EXPECT_TRUE(o5->i_ == 5);
EXPECT_TRUE(o6);
EXPECT_TRUE(o6->i_ == 5);

// Move into empty optional.
o6.reset();
o6 = std::move(o5);
EXPECT_TRUE(o6);
EXPECT_TRUE(o6->i_ == 5);

// Copy into engaged optional.
beman::optional26::optional<not_trivial_copy_assignable> o7{7};
o6 = o7;
EXPECT_TRUE(o6);
EXPECT_TRUE(o6->i_ == 7);

// Move into engaged optional.
beman::optional26::optional<not_trivial_copy_assignable> o8{8};
o6 = std::move(o8);
EXPECT_TRUE(o6);
EXPECT_TRUE(o6->i_ == 8);

// Copy from empty into engaged optional.
o5.reset();
o7 = o5;
EXPECT_FALSE(o7);

// Move from empty into engaged optional.
o8 = std::move(o5);
EXPECT_FALSE(o8);
}

TEST(OptionalTest, Triviality) {
Expand Down

0 comments on commit 08c8477

Please sign in to comment.