Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions modules/yup_core/misc/yup_ResultValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@ class YUP_API ResultValue
return valueOrErrorMessage.index() != 1;
}

/** Returns a copy of the value that was set when this result was created,
or a value constructed from @p defaultValue if the result indicates a failure.

This overload is available when the held value can be copied. Use the
rvalue-qualified overload when working with move-only value types.
*/
template <class U = T>
auto valueOr (U&& defaultValue) const& -> std::enable_if_t<std::is_copy_constructible_v<T> && std::is_constructible_v<T, U&&>, T>
{
if (valueOrErrorMessage.index() == 1)
return std::get<1> (valueOrErrorMessage);

return T (std::forward<U> (defaultValue));
}

/** Returns the moved value that was set when this result was created,
or a value constructed from @p defaultValue if the result indicates a failure.

This overload allows valueOr() to be used with move-only value types when
the ResultValue itself is an rvalue.
*/
template <class U = T>
auto valueOr (U&& defaultValue) && -> std::enable_if_t<std::is_move_constructible_v<T> && std::is_constructible_v<T, U&&>, T>
{
if (valueOrErrorMessage.index() == 1)
return std::get<1> (std::move (valueOrErrorMessage));

return T (std::forward<U> (defaultValue));
}

/** Returns a copy of the value that was set when this result was created. */
T getValue() const&
{
Expand Down
Loading
Loading