Skip to content

Commit 08c8477

Browse files
authored
Merge pull request #97 from jiixyj/set-engaged-flag-on-assignment
Set 'engaged' flag on assignment correctly
2 parents f4cd4b7 + 9a60702 commit 08c8477

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

include/beman/optional26/optional.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ inline constexpr optional<T>::optional(const optional& rhs)
424424
requires std::is_copy_constructible_v<T> && (!std::is_trivially_copy_constructible_v<T>)
425425
{
426426
if (rhs.has_value()) {
427-
std::construct_at(std::addressof(value_), rhs.value_);
428-
engaged_ = true;
427+
construct(rhs.value_);
429428
}
430429
}
431430

@@ -434,8 +433,7 @@ inline constexpr optional<T>::optional(optional&& rhs) noexcept(std::is_nothrow_
434433
requires std::is_move_constructible_v<T> && (!std::is_trivially_move_constructible_v<T>)
435434
{
436435
if (rhs.has_value()) {
437-
std::construct_at(std::addressof(value_), std::move(rhs.value()));
438-
engaged_ = true;
436+
construct(std::move(rhs.value_));
439437
}
440438
}
441439

@@ -466,8 +464,7 @@ inline constexpr optional<T>::optional(const optional<U>& rhs)
466464
requires detail::enable_from_other<T, U, const U&> && std::is_convertible_v<const U&, T>
467465
{
468466
if (rhs.has_value()) {
469-
std::construct_at(std::addressof(value_), rhs.value());
470-
engaged_ = true;
467+
construct(*rhs);
471468
}
472469
}
473470

@@ -524,7 +521,7 @@ inline constexpr optional<T>& optional<T>::operator=(const optional<T>& rhs)
524521
else if (has_value())
525522
value_ = rhs.value_;
526523
else
527-
std::construct_at(std::addressof(value_), rhs.value_);
524+
construct(rhs.value_);
528525
return *this;
529526
}
530527

@@ -539,7 +536,7 @@ optional<T>::operator=(optional<T>&& rhs) noexcept(std::is_nothrow_move_construc
539536
else if (has_value())
540537
value_ = std::move(rhs.value_);
541538
else
542-
std::construct_at(std::addressof(value_), std::move(rhs.value_));
539+
construct(std::move(rhs.value_));
543540
return *this;
544541
}
545542

src/beman/optional26/tests/optional.t.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,38 @@ TEST(OptionalTest, AssignmentValue) {
208208
*/
209209
beman::optional26::optional<not_trivial_copy_assignable> o5{5};
210210
beman::optional26::optional<not_trivial_copy_assignable> o6;
211+
212+
// Copy into empty optional.
211213
o6 = o5;
212-
EXPECT_TRUE(o5->i_ == 5);
214+
EXPECT_TRUE(o6);
215+
EXPECT_TRUE(o6->i_ == 5);
216+
217+
// Move into empty optional.
218+
o6.reset();
219+
o6 = std::move(o5);
220+
EXPECT_TRUE(o6);
221+
EXPECT_TRUE(o6->i_ == 5);
222+
223+
// Copy into engaged optional.
224+
beman::optional26::optional<not_trivial_copy_assignable> o7{7};
225+
o6 = o7;
226+
EXPECT_TRUE(o6);
227+
EXPECT_TRUE(o6->i_ == 7);
228+
229+
// Move into engaged optional.
230+
beman::optional26::optional<not_trivial_copy_assignable> o8{8};
231+
o6 = std::move(o8);
232+
EXPECT_TRUE(o6);
233+
EXPECT_TRUE(o6->i_ == 8);
234+
235+
// Copy from empty into engaged optional.
236+
o5.reset();
237+
o7 = o5;
238+
EXPECT_FALSE(o7);
239+
240+
// Move from empty into engaged optional.
241+
o8 = std::move(o5);
242+
EXPECT_FALSE(o8);
213243
}
214244

215245
TEST(OptionalTest, Triviality) {

0 commit comments

Comments
 (0)