AK: Use Noncopyable.h in Optional<T&>

This makes them trivially copyable/movable, silencing

> "parameter is copied for each invocation"

warnings on `Optional<T&>`, which are unnecessairy,
since `Optional<T&>` is just a trivially copyable pointer.

This creates a slight change in behaviour when moving out of an
`Optional<T&>`, since the moved-from optional no longer gets cleared.

Moved-from values should be considered to be in an undefined state,
and if clearing a moved-from `Optional<T&>` is desired, you should be
using `Optional<T&>::release_value()` instead.
This commit is contained in:
Jonne Ransijn 2024-11-25 12:54:43 +01:00 committed by Ali Mohammad Pur
parent 7e78d7d332
commit 55383998b1
Notes: github-actions[bot] 2024-12-04 00:59:45 +00:00
2 changed files with 3 additions and 31 deletions

View file

@ -336,6 +336,9 @@ private:
template<typename T>
requires(IsLvalueReference<T>) class [[nodiscard]] Optional<T> {
AK_MAKE_DEFAULT_COPYABLE(Optional);
AK_MAKE_DEFAULT_MOVABLE(Optional);
template<typename>
friend class Optional;
@ -369,17 +372,6 @@ public:
{
}
ALWAYS_INLINE Optional(Optional const& other)
: m_pointer(other.m_pointer)
{
}
ALWAYS_INLINE Optional(Optional&& other)
: m_pointer(other.m_pointer)
{
other.m_pointer = nullptr;
}
template<typename U>
ALWAYS_INLINE Optional(Optional<U>& other)
requires(CanBePlacedInOptional<U>)
@ -402,25 +394,6 @@ public:
other.m_pointer = nullptr;
}
ALWAYS_INLINE Optional& operator=(Optional& other)
{
m_pointer = other.m_pointer;
return *this;
}
ALWAYS_INLINE Optional& operator=(Optional const& other)
{
m_pointer = other.m_pointer;
return *this;
}
ALWAYS_INLINE Optional& operator=(Optional&& other)
{
m_pointer = other.m_pointer;
other.m_pointer = nullptr;
return *this;
}
template<typename U>
ALWAYS_INLINE Optional& operator=(Optional<U>& other)
requires(CanBePlacedInOptional<U>)

View file

@ -247,7 +247,6 @@ TEST_CASE(move_optional_reference)
y = move(x);
EXPECT_EQ(y.has_value(), true);
EXPECT_EQ(y.value(), 3);
EXPECT_EQ(x.has_value(), false);
}
TEST_CASE(short_notation_reference)