Add Optional::map and Optional::cast methods

Cool kids call that monadic interface if I understood correctly.
This commit is contained in:
Maxime Coste 2019-04-01 22:09:32 +11:00
parent 0b758909be
commit b8cf457e82

View File

@ -81,6 +81,26 @@ public:
}
const T* operator->() const { return const_cast<Optional&>(*this).operator->(); }
template<typename U> struct DecayOptionalImpl { using Type = U; };
template<typename U> struct DecayOptionalImpl<Optional<U>> { using Type = typename DecayOptionalImpl<U>::Type; };
template<typename U> using DecayOptional = typename DecayOptionalImpl<U>::Type;
template<typename F>
auto map(F f) -> Optional<DecayOptional<decltype(f(std::declval<T&&>()))>>
{
if (not m_valid)
return {};
return {f(m_value)};
}
template<typename U>
auto cast() -> Optional<U>
{
if (not m_valid)
return {};
return {(U)m_value};
}
template<typename U>
T value_or(U&& fallback) const { return m_valid ? m_value : T{std::forward<U>(fallback)}; }