Skip to content

Commit

Permalink
Merge pull request bemanproject#95 from jiixyj/missing-nullopt-assign…
Browse files Browse the repository at this point in the history
…ment

Add missing assignment operator from 'nullopt_t'
  • Loading branch information
neatudarius authored Jan 4, 2025
2 parents 08c8477 + e280641 commit 5460182
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/beman/optional26/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ class optional {
requires(!std::is_trivially_destructible_v<T>);

// \ref{optional.assign}, assignment
constexpr optional& operator=(nullopt_t) noexcept;

constexpr optional& operator=(const optional& rhs)
requires std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T> &&
(!std::is_trivially_copy_assignable_v<T>);
Expand Down Expand Up @@ -511,6 +513,12 @@ inline constexpr optional<T>::~optional()

// 22.5.3.4 Assignment[optional.assign]

template <class T>
inline constexpr optional<T>& optional<T>::operator=(nullopt_t) noexcept {
reset();
return *this;
}

template <class T>
inline constexpr optional<T>& optional<T>::operator=(const optional<T>& rhs)
requires std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T> &&
Expand Down
16 changes: 16 additions & 0 deletions src/beman/optional26/tests/optional.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,19 @@ TEST(OptionalTest, HashTest) {
EXPECT_EQ(h1, h2);
}
}

TEST(OptionalTest, CanHoldValueOfImmovableType) {
using beman::optional26::tests::immovable;

beman::optional26::optional<immovable> o1(beman::optional26::in_place);
EXPECT_TRUE(o1);

// ...and can reset it with `nullopt`.
static_assert(noexcept(o1 = beman::optional26::nullopt));
o1 = beman::optional26::nullopt;
EXPECT_FALSE(o1);

// Also, can construct with `nullopt`.
beman::optional26::optional<immovable> o2 = beman::optional26::nullopt;
EXPECT_FALSE(o2);
}
6 changes: 6 additions & 0 deletions src/beman/optional26/tests/test_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class Point {
bool operator==(const Point&) const = default;
};

struct immovable {
explicit immovable() = default;
immovable(const immovable&) = delete;
immovable& operator=(const immovable&) = delete;
};

} // namespace beman::optional26::tests

#endif // BEMAN_OPTIONAL26_TESTS_TEST_TYPES_HPP

0 comments on commit 5460182

Please sign in to comment.