BAN: Optional can be constructed inplace

This commit is contained in:
Bananymous 2023-06-12 22:33:04 +03:00
parent 97638f7ade
commit c38e8de6b5
1 changed files with 26 additions and 0 deletions

View File

@ -15,11 +15,17 @@ namespace BAN
Optional();
Optional(const T&);
Optional(T&&);
template<typename... Args>
Optional(Args&&...);
~Optional();
Optional& operator=(const Optional&);
Optional& operator=(Optional&&);
template<typename... Args>
Optional& emplace(Args&&...);
T* operator->();
const T* operator->() const;
@ -58,6 +64,14 @@ namespace BAN
new (m_storage) T(BAN::move(value));
}
template<typename T>
template<typename... Args>
Optional<T>::Optional(Args&&... args)
: m_has_value(true)
{
new (m_storage) T(BAN::forward<Args>(args)...);
}
template<typename T>
Optional<T>::~Optional()
{
@ -73,6 +87,7 @@ namespace BAN
m_has_value = true;
new (m_storage) T(other.value());
}
return *this;
}
template<typename T>
@ -84,6 +99,17 @@ namespace BAN
m_has_value = true;
new (m_storage) T(BAN::move(other.release_value()));
}
return *this;
}
template<typename T>
template<typename... Args>
Optional<T>& Optional<T>::emplace(Args&&... args)
{
clear();
m_has_value = true;
new (m_storage) T(BAN::forward<Args>(args)...);
return *this;
}
template<typename T>