Skip to content

Commit 216542b

Browse files
committed
Add missing assignment operator from 'nullopt_t'
1 parent f4cd4b7 commit 216542b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

include/beman/optional26/optional.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ class optional {
299299
requires(!std::is_trivially_destructible_v<T>);
300300

301301
// \ref{optional.assign}, assignment
302+
constexpr optional& operator=(nullopt_t) noexcept;
303+
302304
constexpr optional& operator=(const optional& rhs)
303305
requires std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T> &&
304306
(!std::is_trivially_copy_assignable_v<T>);
@@ -514,6 +516,12 @@ inline constexpr optional<T>::~optional()
514516

515517
// 22.5.3.4 Assignment[optional.assign]
516518

519+
template <class T>
520+
inline constexpr optional<T>& optional<T>::operator=(nullopt_t) noexcept {
521+
reset();
522+
return *this;
523+
}
524+
517525
template <class T>
518526
inline constexpr optional<T>& optional<T>::operator=(const optional<T>& rhs)
519527
requires std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T> &&

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,3 +868,23 @@ TEST(OptionalTest, HashTest) {
868868
EXPECT_EQ(h1, h2);
869869
}
870870
}
871+
872+
TEST(OptionalTest, CanHoldValueOfImmovableType) {
873+
struct immovable {
874+
explicit immovable() = default;
875+
immovable(const immovable&) = delete;
876+
immovable& operator=(const immovable&) = delete;
877+
};
878+
879+
beman::optional26::optional<immovable> o1(beman::optional26::in_place);
880+
EXPECT_TRUE(o1);
881+
882+
// ...and can reset it with `nullopt`.
883+
static_assert(noexcept(o1 = beman::optional26::nullopt));
884+
o1 = beman::optional26::nullopt;
885+
EXPECT_FALSE(o1);
886+
887+
// Also, can construct with `nullopt`.
888+
beman::optional26::optional<immovable> o2 = beman::optional26::nullopt;
889+
EXPECT_FALSE(o2);
890+
}

0 commit comments

Comments
 (0)