From 518fb6d2087d4bc666a2f220441863d742c5f3e4 Mon Sep 17 00:00:00 2001 From: Scott Determan Date: Mon, 28 Nov 2022 17:01:54 -0500 Subject: [PATCH] Improve move semantics in Expected (#4326) * Improve move semantics in Expected: This patch unconditionally moves an `Unexpected` value parameter as long as `U` is not a reference. If `U` is a reference the code should not compile. An error type that holds a reference is a strange use-case, and an overload is not provided. If it is required in the future it can be added. The `Expected(U r)` overload should take a forwarding ref. * Replace enable_if with concepts in Expected --- src/ripple/basics/Expected.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/ripple/basics/Expected.h b/src/ripple/basics/Expected.h index 09d2bdc5042..8dc368eefd7 100644 --- a/src/ripple/basics/Expected.h +++ b/src/ripple/basics/Expected.h @@ -21,7 +21,10 @@ #define RIPPLE_BASICS_EXPECTED_H_INCLUDED #include + #include + +#include #include #include @@ -132,17 +135,16 @@ class [[nodiscard]] Expected using Base = boost::outcome_v2::result; public: - template < - typename U, - typename = std::enable_if_t>> - constexpr Expected(U r) : Base(T{std::forward(r)}) + template + requires std::convertible_to constexpr Expected(U && r) + : Base(T{std::forward(r)}) { } - template < - typename U, - typename = std::enable_if_t>> - constexpr Expected(Unexpected e) : Base(E{std::forward(e.value())}) + template + requires std::convertible_to && + (!std::is_reference_v)constexpr Expected(Unexpected e) + : Base(E{std::move(e.value())}) { } @@ -215,10 +217,10 @@ class [[nodiscard]] Expected { } - template < - typename U, - typename = std::enable_if_t>> - constexpr Expected(Unexpected e) : Base(E{std::forward(e.value())}) + template + requires std::convertible_to && + (!std::is_reference_v)constexpr Expected(Unexpected e) + : Base(E{std::move(e.value())}) { }