Skip to content

Commit 5460182

Browse files
authored
Merge pull request #95 from jiixyj/missing-nullopt-assignment
Add missing assignment operator from 'nullopt_t'
2 parents 08c8477 + e280641 commit 5460182

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-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>);
@@ -511,6 +513,12 @@ inline constexpr optional<T>::~optional()
511513

512514
// 22.5.3.4 Assignment[optional.assign]
513515

516+
template <class T>
517+
inline constexpr optional<T>& optional<T>::operator=(nullopt_t) noexcept {
518+
reset();
519+
return *this;
520+
}
521+
514522
template <class T>
515523
inline constexpr optional<T>& optional<T>::operator=(const optional<T>& rhs)
516524
requires std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T> &&

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,3 +898,19 @@ TEST(OptionalTest, HashTest) {
898898
EXPECT_EQ(h1, h2);
899899
}
900900
}
901+
902+
TEST(OptionalTest, CanHoldValueOfImmovableType) {
903+
using beman::optional26::tests::immovable;
904+
905+
beman::optional26::optional<immovable> o1(beman::optional26::in_place);
906+
EXPECT_TRUE(o1);
907+
908+
// ...and can reset it with `nullopt`.
909+
static_assert(noexcept(o1 = beman::optional26::nullopt));
910+
o1 = beman::optional26::nullopt;
911+
EXPECT_FALSE(o1);
912+
913+
// Also, can construct with `nullopt`.
914+
beman::optional26::optional<immovable> o2 = beman::optional26::nullopt;
915+
EXPECT_FALSE(o2);
916+
}

src/beman/optional26/tests/test_types.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class Point {
6565
bool operator==(const Point&) const = default;
6666
};
6767

68+
struct immovable {
69+
explicit immovable() = default;
70+
immovable(const immovable&) = delete;
71+
immovable& operator=(const immovable&) = delete;
72+
};
73+
6874
} // namespace beman::optional26::tests
6975

7076
#endif // BEMAN_OPTIONAL26_TESTS_TEST_TYPES_HPP

0 commit comments

Comments
 (0)