@@ -878,6 +878,17 @@ class ReturnAction final {
878878 public:
879879 explicit ReturnAction (R value) : value_(std::move(value)) {}
880880
881+ template <typename U, typename ... Args,
882+ typename = typename std::enable_if<conjunction<
883+ // See the requirements documented on Return.
884+ negation<std::is_same<void , U>>, //
885+ negation<std::is_reference<U>>, //
886+ std::is_convertible<R, U>, //
887+ std::is_move_constructible<U>>::value>::type>
888+ operator OnceAction<U(Args...)>() && { // NOLINT
889+ return Impl<U>(std::move (value_));
890+ }
891+
881892 template <typename U, typename ... Args,
882893 typename = typename std::enable_if<conjunction<
883894 // See the requirements documented on Return.
@@ -894,9 +905,17 @@ class ReturnAction final {
894905 template <typename U>
895906 class Impl final {
896907 public:
908+ // The constructor used when the return value is allowed to move from the
909+ // input value (i.e. we are converting to OnceAction).
910+ explicit Impl (R&& input_value)
911+ : state_(new State(std::move(input_value))) {}
912+
913+ // The constructor used when the return value is not allowed to move from
914+ // the input value (i.e. we are converting to Action).
897915 explicit Impl (const R& input_value) : state_(new State(input_value)) {}
898916
899- U operator ()() const { return state_->value ; }
917+ U operator ()() && { return std::move (state_->value ); }
918+ U operator ()() const & { return state_->value ; }
900919
901920 private:
902921 // We put our state on the heap so that the compiler-generated copy/move
@@ -923,6 +942,18 @@ class ReturnAction final {
923942 // explicit constructor from R.
924943 value(ImplicitCast_<U>(internal::as_const(input_value))) {}
925944
945+ // As above, but for the case where we're moving from the ReturnAction
946+ // object because it's being used as a OnceAction.
947+ explicit State (R&& input_value_in)
948+ : input_value(std::move(input_value_in)),
949+ // For the same reason as above we make an implicit conversion to U
950+ // before initializing the value.
951+ //
952+ // Unlike above we provide the input value as an rvalue to the
953+ // implicit conversion because this is a OnceAction: it's fine if it
954+ // wants to consume the input value.
955+ value(ImplicitCast_<U>(std::move(input_value))) {}
956+
926957 // A copy of the value originally provided by the user. We retain this in
927958 // addition to the value of the mock function's result type below in case
928959 // the latter is a reference-like type. See the std::string_view example
@@ -1740,18 +1771,19 @@ internal::WithArgsAction<typename std::decay<InnerAction>::type> WithoutArgs(
17401771
17411772// Creates an action that returns a value.
17421773//
1743- // R must be copy-constructible. The returned type can be used as an
1744- // Action<U(Args...)> for any type U where all of the following are true :
1774+ // The returned type can be used with a mock function returning a non-void,
1775+ // non-reference type U as follows :
17451776//
1746- // * U is not void.
1747- // * U is not a reference type. (Use ReturnRef instead.)
1748- // * U is copy-constructible.
1749- // * const R& is convertible to U.
1777+ // * If R is convertible to U and U is move-constructible, then the action can
1778+ // be used with WillOnce.
17501779//
1751- // The Action<U(Args)...> object contains the R value from which the U return
1752- // value is constructed (a copy of the argument to Return). This means that the
1753- // R value will survive at least until the mock object's expectations are
1754- // cleared or the mock object is destroyed, meaning that U can be a
1780+ // * If const R& is convertible to U and U is copy-constructible, then the
1781+ // action can be used with both WillOnce and WillRepeatedly.
1782+ //
1783+ // The mock expectation contains the R value from which the U return value is
1784+ // constructed (a move/copy of the argument to Return). This means that the R
1785+ // value will survive at least until the mock object's expectations are cleared
1786+ // or the mock object is destroyed, meaning that U can safely be a
17551787// reference-like type such as std::string_view:
17561788//
17571789// // The mock function returns a view of a copy of the string fed to
@@ -1794,6 +1826,8 @@ inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) {
17941826 return internal::ReturnRefOfCopyAction<R>(x);
17951827}
17961828
1829+ // DEPRECATED: use Return(x) directly with WillOnce.
1830+ //
17971831// Modifies the parent action (a Return() action) to perform a move of the
17981832// argument instead of a copy.
17991833// Return(ByMove()) actions can only be executed once and will assert this
0 commit comments