Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading