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
80 changes: 68 additions & 12 deletions include/beman/optional26/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,19 +276,29 @@ class optional {

template <class U>
constexpr explicit(!std::is_convertible_v<U, T>) optional(const optional<U>& rhs)
requires detail::enable_from_other<T, U, const U&> && std::is_convertible_v<const U&, T>;
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, const U&> &&
std::is_convertible_v<const U&, T>);

template <class U>
constexpr explicit(!std::is_convertible_v<U, T>) optional(const optional<U>& rhs)
requires detail::enable_from_other<T, U, const U&> && (!std::is_convertible_v<const U&, T>);
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, const U&> &&
!std::is_convertible_v<const U&, T>);

template <class U>
constexpr explicit(!std::is_convertible_v<U, T>) optional(optional<U>&& rhs)
requires detail::enable_from_other<T, U, U&&> && std::is_convertible_v<U&&, T>;
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, U &&> && std::is_convertible_v<U &&, T>);

template <class U>
constexpr explicit(!std::is_convertible_v<U, T>) optional(optional<U>&& rhs)
requires detail::enable_from_other<T, U, U&&> && (!std::is_convertible_v<U &&, T>);
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, U &&> && !std::is_convertible_v<U &&, T>);

template <class U>
constexpr explicit(!std::is_convertible_v<U&, T>) optional(const optional<U&>& rhs)
requires(detail::enable_from_other<T, U&, U&> && std::is_convertible_v<U&, T>);

template <class U>
constexpr explicit(!std::is_convertible_v<U&, T>) optional(const optional<U&>& rhs)
requires(detail::enable_from_other<T, U&, U&> && !std::is_convertible_v<U&, T>);

// \ref{optional.dtor}, destructor
constexpr ~optional()
Expand Down Expand Up @@ -325,11 +335,15 @@ class optional {

template <class U>
constexpr optional& operator=(const optional<U>& rhs)
requires detail::enable_assign_from_other<T, U, const U&>;
requires(!std::is_reference_v<U> && detail::enable_assign_from_other<T, U, const U&>);

template <class U>
constexpr optional& operator=(optional<U>&& rhs)
requires detail::enable_assign_from_other<T, U, U>;
requires(!std::is_reference_v<U> && detail::enable_assign_from_other<T, U, U>);

template <class U>
constexpr optional& operator=(const optional<U&>& rhs)
requires(detail::enable_assign_from_other<T, U&, U&>);

template <class... Args>
constexpr T& emplace(Args&&... args);
Expand Down Expand Up @@ -463,7 +477,8 @@ inline constexpr optional<T>::optional(U&& u)
template <class T>
template <class U>
inline constexpr optional<T>::optional(const optional<U>& rhs)
requires detail::enable_from_other<T, U, const U&> && std::is_convertible_v<const U&, T>
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, const U&> &&
std::is_convertible_v<const U&, T>)
{
if (rhs.has_value()) {
construct(*rhs);
Expand All @@ -473,7 +488,8 @@ inline constexpr optional<T>::optional(const optional<U>& rhs)
template <class T>
template <class U>
inline constexpr optional<T>::optional(const optional<U>& rhs)
requires detail::enable_from_other<T, U, const U&> && (!std::is_convertible_v<const U&, T>)
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, const U&> &&
!std::is_convertible_v<const U&, T>)
{
if (rhs.has_value()) {
construct(*rhs);
Expand All @@ -484,7 +500,7 @@ inline constexpr optional<T>::optional(const optional<U>& rhs)
template <class T>
template <class U>
inline constexpr optional<T>::optional(optional<U>&& rhs)
requires detail::enable_from_other<T, U, U&&> && std::is_convertible_v<U&&, T>
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, U &&> && std::is_convertible_v<U &&, T>)
{
if (rhs.has_value()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://eel.is/c++draft/optional#ctor-34 means this needs a paper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, and there is https://eel.is/c++draft/optional#assign-25 as well.

Although in a world where only optional<T> exists I believe std::move(*rhs) and *std::move(rhs) do the same thing. So implementers could have used the as-if rule this whole time. Only for optional<T&> there might be a semantic difference. Maybe P2988 is a good vehicle for this?

construct(std::move(*rhs));
Expand All @@ -494,13 +510,33 @@ inline constexpr optional<T>::optional(optional<U>&& rhs)
template <class T>
template <class U>
inline constexpr optional<T>::optional(optional<U>&& rhs)
requires detail::enable_from_other<T, U, U&&> && (!std::is_convertible_v<U &&, T>)
requires(!std::is_reference_v<U> && detail::enable_from_other<T, U, U &&> && !std::is_convertible_v<U &&, T>)
{
if (rhs.has_value()) {
construct(std::move(*rhs));
}
}

template <class T>
template <class U>
inline constexpr optional<T>::optional(const optional<U&>& rhs)
requires(detail::enable_from_other<T, U&, U&> && std::is_convertible_v<U&, T>)
{
if (rhs.has_value()) {
construct(*rhs);
}
}

template <class T>
template <class U>
inline constexpr optional<T>::optional(const optional<U&>& rhs)
requires(detail::enable_from_other<T, U&, U&> && !std::is_convertible_v<U&, T>)
{
if (rhs.has_value()) {
construct(*rhs);
}
}

// 22.5.3.3 Destructor[optional.dtor]

template <class T>
Expand Down Expand Up @@ -571,7 +607,7 @@ inline constexpr optional<T>& optional<T>::operator=(U&& u)
template <class T>
template <class U>
inline constexpr optional<T>& optional<T>::operator=(const optional<U>& rhs)
requires detail::enable_assign_from_other<T, U, const U&>
requires(!std::is_reference_v<U> && detail::enable_assign_from_other<T, U, const U&>)
{
if (has_value()) {
if (rhs.has_value()) {
Expand All @@ -595,7 +631,7 @@ inline constexpr optional<T>& optional<T>::operator=(const optional<U>& rhs)
template <class T>
template <class U>
inline constexpr optional<T>& optional<T>::operator=(optional<U>&& rhs)
requires detail::enable_assign_from_other<T, U, U>
requires(!std::is_reference_v<U> && detail::enable_assign_from_other<T, U, U>)
{
if (has_value()) {
if (rhs.has_value()) {
Expand All @@ -612,6 +648,26 @@ inline constexpr optional<T>& optional<T>::operator=(optional<U>&& rhs)
return *this;
}

template <class T>
template <class U>
inline constexpr optional<T>& optional<T>::operator=(const optional<U&>& rhs)
requires(detail::enable_assign_from_other<T, U&, U&>)
{
if (has_value()) {
if (rhs.has_value()) {
value_ = *rhs;
} else {
hard_reset();
}
}

else if (rhs.has_value()) {
construct(*rhs);
}

return *this;
}

/// Constructs the value in-place, destroying the current one if there is
/// one.
template <class T>
Expand Down
91 changes: 91 additions & 0 deletions src/beman/optional26/tests/optional.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,94 @@ TEST(OptionalTest, CanHoldValueOfImmovableType) {
beman::optional26::optional<immovable> o2 = beman::optional26::nullopt;
EXPECT_FALSE(o2);
}

// Moving an `optional<T&>` should not move the remote value.
TEST(OptionalTest, OptionalFromOptionalRef) {
using beman::optional26::tests::copyable_from_non_const_lvalue_only;

copyable_from_non_const_lvalue_only cm;

beman::optional26::optional<copyable_from_non_const_lvalue_only&> o1 = cm;
ASSERT_TRUE(o1);

{
beman::optional26::optional<copyable_from_non_const_lvalue_only> o2 = o1;
ASSERT_TRUE(o2);
}

beman::optional26::optional<copyable_from_non_const_lvalue_only> o2 = std::move(o1);
ASSERT_TRUE(o2);

o2 = o1;
ASSERT_TRUE(o2);

o2 = std::move(o1);
ASSERT_TRUE(o2);

o2.reset();
o2 = o1;
ASSERT_TRUE(o2);

o2.reset();
o2 = std::move(o1);
ASSERT_TRUE(o2);
}

TEST(OptionalTest, OptionalFromOptionalRefExplicit) {
using beman::optional26::tests::copyable_from_non_const_lvalue_only;
using beman::optional26::tests::explicitly_convertible_from_non_const_lvalue_only;

explicitly_convertible_from_non_const_lvalue_only ec;

beman::optional26::optional<explicitly_convertible_from_non_const_lvalue_only&> o3 = ec;

beman::optional26::optional<copyable_from_non_const_lvalue_only> o4(o3);
ASSERT_TRUE(o4);
beman::optional26::optional<copyable_from_non_const_lvalue_only> o5(std::move(o3));
ASSERT_TRUE(o5);
}

TEST(OptionalTest, OptionalFromOptionalConstRef) {
using beman::optional26::tests::copyable_from_const_lvalue_only;

copyable_from_const_lvalue_only cm;

beman::optional26::optional<const copyable_from_const_lvalue_only&> o1 = cm;
ASSERT_TRUE(o1);

{
beman::optional26::optional<copyable_from_const_lvalue_only> o2 = o1;
ASSERT_TRUE(o2);
}

beman::optional26::optional<copyable_from_const_lvalue_only> o2 = std::move(o1);
ASSERT_TRUE(o2);

o2 = o1;
ASSERT_TRUE(o2);

o2 = std::move(o1);
ASSERT_TRUE(o2);

o2.reset();
o2 = o1;
ASSERT_TRUE(o2);

o2.reset();
o2 = std::move(o1);
ASSERT_TRUE(o2);
}

TEST(OptionalTest, OptionalFromOptionalConstRefExplicit) {
using beman::optional26::tests::copyable_from_const_lvalue_only;
using beman::optional26::tests::explicitly_convertible_from_const_lvalue_only;

explicitly_convertible_from_const_lvalue_only ec;

beman::optional26::optional<const explicitly_convertible_from_const_lvalue_only&> o3 = ec;

beman::optional26::optional<copyable_from_const_lvalue_only> o4(o3);
ASSERT_TRUE(o4);
beman::optional26::optional<copyable_from_const_lvalue_only> o5(std::move(o3));
ASSERT_TRUE(o5);
}
38 changes: 38 additions & 0 deletions src/beman/optional26/tests/test_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ struct immovable {
immovable& operator=(const immovable&) = delete;
};

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

struct explicitly_convertible_from_non_const_lvalue_only {
explicit operator copyable_from_non_const_lvalue_only() & { return copyable_from_non_const_lvalue_only{}; }
explicit operator copyable_from_non_const_lvalue_only() const& = delete;
explicit operator copyable_from_non_const_lvalue_only() && = delete;
explicit operator copyable_from_non_const_lvalue_only() const&& = delete;
};

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

struct explicitly_convertible_from_const_lvalue_only {
explicit operator copyable_from_const_lvalue_only() & = delete;
explicit operator copyable_from_const_lvalue_only() const& { return copyable_from_const_lvalue_only{}; }
explicit operator copyable_from_const_lvalue_only() && = delete;
explicit operator copyable_from_const_lvalue_only() const&& = delete;
};

} // namespace beman::optional26::tests

#endif // BEMAN_OPTIONAL26_TESTS_TEST_TYPES_HPP
Loading