Commit 4eb2d18a authored by Dominik Charousset's avatar Dominik Charousset

Add move_if_optional utility function

This function allows writing generic code that accepts both pointers and
optionals but takes ownership of the managed value to avoid unnecessary
copies.
parent b3b8fc9b
...@@ -305,6 +305,20 @@ std::string to_string(const optional<T>& x) { ...@@ -305,6 +305,20 @@ std::string to_string(const optional<T>& x) {
return x ? "*" + deep_to_string(*x) : "none"; return x ? "*" + deep_to_string(*x) : "none";
} }
/// Returns an rvalue to the value managed by `x`.
/// @relates optional
template <class T>
T&& move_if_optional(optional<T>& x) {
return std::move(*x);
}
/// Returns `*x`.
/// @relates optional
template <class T>
T& move_if_optional(T* x) {
return *x;
}
// -- [X.Y.8] comparison with optional ---------------------------------------- // -- [X.Y.8] comparison with optional ----------------------------------------
/// @relates optional /// @relates optional
...@@ -493,4 +507,3 @@ bool operator>=(const T& lhs, const optional<T>& rhs) { ...@@ -493,4 +507,3 @@ bool operator>=(const T& lhs, const optional<T>& rhs) {
} }
} // namespace caf } // namespace caf
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment