Skip to content

Commit fa14f9d

Browse files
committed
Merge remote-tracking branch 'origin/main' into missing-nullopt-assignment
2 parents 216542b + 08c8477 commit fa14f9d

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
@@ -426,8 +426,7 @@ inline constexpr optional<T>::optional(const optional& rhs)
426426
requires std::is_copy_constructible_v<T> && (!std::is_trivially_copy_constructible_v<T>)
427427
{
428428
if (rhs.has_value()) {
429-
std::construct_at(std::addressof(value_), rhs.value_);
430-
engaged_ = true;
429+
construct(rhs.value_);
431430
}
432431
}
433432

@@ -436,8 +435,7 @@ inline constexpr optional<T>::optional(optional&& rhs) noexcept(std::is_nothrow_
436435
requires std::is_move_constructible_v<T> && (!std::is_trivially_move_constructible_v<T>)
437436
{
438437
if (rhs.has_value()) {
439-
std::construct_at(std::addressof(value_), std::move(rhs.value()));
440-
engaged_ = true;
438+
construct(std::move(rhs.value_));
441439
}
442440
}
443441

@@ -468,8 +466,7 @@ inline constexpr optional<T>::optional(const optional<U>& rhs)
468466
requires detail::enable_from_other<T, U, const U&> && std::is_convertible_v<const U&, T>
469467
{
470468
if (rhs.has_value()) {
471-
std::construct_at(std::addressof(value_), rhs.value());
472-
engaged_ = true;
469+
construct(*rhs);
473470
}
474471
}
475472

@@ -532,7 +529,7 @@ inline constexpr optional<T>& optional<T>::operator=(const optional<T>& rhs)
532529
else if (has_value())
533530
value_ = rhs.value_;
534531
else
535-
std::construct_at(std::addressof(value_), rhs.value_);
532+
construct(rhs.value_);
536533
return *this;
537534
}
538535

@@ -547,7 +544,7 @@ optional<T>::operator=(optional<T>&& rhs) noexcept(std::is_nothrow_move_construc
547544
else if (has_value())
548545
value_ = std::move(rhs.value_);
549546
else
550-
std::construct_at(std::addressof(value_), std::move(rhs.value_));
547+
construct(std::move(rhs.value_));
551548
return *this;
552549
}
553550

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)