Add perfect forwarding to Optional::map and Optional::orDefault

This commit is contained in:
sfc-gh-tclinkenbeard 2021-07-12 02:33:51 -07:00
parent 90c6d76349
commit 2276397159
1 changed files with 14 additions and 7 deletions

View File

@ -237,12 +237,12 @@ public:
}
template <class R>
Optional<R> map(std::function<R(T)> f) const {
if (present()) {
return Optional<R>(f(get()));
} else {
return Optional<R>();
}
Optional<R> map(std::function<R(T)> f) const& {
return present() ? Optional<R>(f(get())) : Optional<R>();
}
template <class R>
Optional<R> map(std::function<R(T)> f) && {
return present() ? Optional<R>(f(std::move(*this).get())) : Optional<R>();
}
bool present() const { return impl.has_value(); }
@ -258,7 +258,14 @@ public:
UNSTOPPABLE_ASSERT(impl.has_value());
return std::move(impl.value());
}
T orDefault(T const& default_value) const { return impl.value_or(default_value); }
template <class U>
T orDefault(U&& defaultValue) const& {
return impl.value_or(std::forward<U>(defaultValue));
}
template <class U>
T orDefault(U&& defaultValue) && {
return std::move(impl).value_or(std::forward<U>(defaultValue));
}
// Spaceship operator. Treats not-present as less-than present.
int compare(Optional const& rhs) const {