(f)()`.
+### Function template `tl::optional::map_or`
+
+(1) template <class F, class U>
+ U map_or(F&& f, U&& u) &;
+
+(2) template <class F, class U>
+ U map_or(F&& f, U&& u) const &;
+
+Maps the stored value with `f` if there is one, otherwise returns `u`
+
+If there is a value stored, then `f` is called with `**this` and the value is returned. Otherwise `u` is returned.
+
+### Function template `tl::optional::map_or`
+
+(1) template <class F, class U>
+ U map_or(F&& f, U&& u) &&;
+
+(2) template <class F, class U>
+ U map_or(F&& f, U&& u) const &&;
+
+Maps the stored value with `f` if there is one, otherwise returns `u`
+
+If there is a value stored, then `f` is called with `std::move(**this)` and the value is returned. Otherwise `u` is returned.
+
+### Function template `tl::optional::map_or_else`
+
+(1) template <class F, class U>
+ U map_or_else(F&& f, U&& u) &;
+
+(2) template <class F, class U>
+ U map_or_else(F&& f, U&& u) const &;
+
+Maps the stored value with `f` if there is one, otherwise calls `u` and returns the result.
+
+If there is a value stored, then `f` is called with `**this` and the value is returned. Otherwise `std::forward(u)()` is returned.
+
+### Function template `tl::optional::map_or_else`
+
+(1) template <class F, class U>
+ U map_or_else(F&& f, U&& u) &&;
+
+(2) template <class F, class U>
+ U map_or_else(F&& f, U&& u) const &&;
+
+Maps the stored value with `f` if there is one, otherwise calls `u` and returns the result.
+
+If there is a value stored, then `f` is called with `std::move(**this)` and the value is returned. Otherwise `std::forward(u)()` is returned.
+
-----
-----
diff --git a/optional.hpp b/optional.hpp
index 90389c6..2d8e328 100644
--- a/optional.hpp
+++ b/optional.hpp
@@ -1124,41 +1124,62 @@ public:
return has_value() ? std::move(*this) : std::forward(f)();
}
+
+ /// \group map_or
+ /// \brief Maps the stored value with `f` if there is one, otherwise returns `u`
+ /// \details If there is a value stored, then `f` is called with `**this` and the value is returned.
+ /// Otherwise `u` is returned.
template U map_or(F &&f, U &&u) & {
return has_value() ? detail::invoke(std::forward(f), **this)
: std::forward(u);
}
+ /// \group map_or_val
+ /// \brief Maps the stored value with `f` if there is one, otherwise returns `u`
+ /// \details If there is a value stored, then `f` is called with `std::move(**this)` and the value is returned.
+ /// Otherwise `u` is returned.
template U map_or(F &&f, U &&u) && {
return has_value() ? detail::invoke(std::forward(f), std::move(**this))
: std::forward(u);
}
+ /// \group map_or
template U map_or(F &&f, U &&u) const & {
return has_value() ? detail::invoke(std::forward(f), **this)
: std::forward(u);
}
+ /// \group map_or_val
template U map_or(F &&f, U &&u) const && {
return has_value() ? detail::invoke(std::forward(f), std::move(**this))
: std::forward(u);
}
+ /// \group map_or_else
+ /// \brief Maps the stored value with `f` if there is one, otherwise calls `u` and returns the result.
+ /// \details If there is a value stored, then `f` is called with `**this` and the value is returned.
+ /// Otherwise `std::forward(u)()` is returned.
template U map_or_else(F &&f, U &&u) & {
return has_value() ? detail::invoke(std::forward(f), **this)
: std::forward(u)();
}
+ /// \group map_or_else_rval
+ /// \brief Maps the stored value with `f` if there is one, otherwise calls `u` and returns the result.
+ /// \details If there is a value stored, then `f` is called with `std::move(**this)` and the value is returned.
+ /// Otherwise `std::forward(u)()` is returned.
template U map_or_else(F &&f, U &&u) && {
return has_value() ? detail::invoke(std::forward(f), std::move(**this))
: std::forward(u)();
}
+ /// \group map_or_else
template U map_or_else(F &&f, U &&u) const & {
return has_value() ? detail::invoke(std::forward(f), **this)
: std::forward(u)();
}
+ /// \group map_or_else_rval
template U map_or_else(F &&f, U &&u) const && {
return has_value() ? detail::invoke(std::forward(f), std::move(**this))
: std::forward(u)();