Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set 'engaged' flag on assignment correctly #97

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading