AK: Make Optional<T> explicitly constructible from Optional<U>
As long as T is constructible from U. This allows us to avoid patterns like `abc.has_value() ? Optional<U>(abc.value()) : Optional<U>()`.
This commit is contained in:
parent
fb06d494f0
commit
a3a4d0aea2
Notes:
sideshowbarker
2024-07-17 20:22:50 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/a3a4d0aea2e Pull-request: https://github.com/SerenityOS/serenity/pull/12090 Reviewed-by: https://github.com/alimpfard ✅ Reviewed-by: https://github.com/linusg ✅
1 changed files with 21 additions and 4 deletions
|
@ -24,6 +24,9 @@ namespace AK {
|
|||
|
||||
template<typename T>
|
||||
class [[nodiscard]] Optional {
|
||||
template<typename U>
|
||||
friend class Optional;
|
||||
|
||||
public:
|
||||
using ValueType = T;
|
||||
|
||||
|
@ -50,17 +53,31 @@ public:
|
|||
#endif
|
||||
: m_has_value(other.m_has_value)
|
||||
{
|
||||
if (other.has_value()) {
|
||||
if (other.has_value())
|
||||
new (&m_storage) T(other.value());
|
||||
}
|
||||
}
|
||||
|
||||
ALWAYS_INLINE Optional(Optional&& other)
|
||||
: m_has_value(other.m_has_value)
|
||||
{
|
||||
if (other.has_value()) {
|
||||
if (other.has_value())
|
||||
new (&m_storage) T(other.release_value());
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
requires(IsConstructible<T, U const&> && !IsSpecializationOf<T, Optional> && !IsSpecializationOf<U, Optional>) ALWAYS_INLINE explicit Optional(Optional<U> const& other)
|
||||
: m_has_value(other.m_has_value)
|
||||
{
|
||||
if (other.has_value())
|
||||
new (&m_storage) T(other.value());
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
requires(IsConstructible<T, U&&> && !IsSpecializationOf<T, Optional> && !IsSpecializationOf<U, Optional>) ALWAYS_INLINE explicit Optional(Optional<U>&& other)
|
||||
: m_has_value(other.m_has_value)
|
||||
{
|
||||
if (other.has_value())
|
||||
new (&m_storage) T(other.release_value());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename U = T>
|
||||
|
|
Loading…
Add table
Reference in a new issue