From e4fdb87e76b9fc4b01c54ad81aea19d6e994b994 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 29 Feb 2024 07:41:40 -0800 Subject: Accept one-shot callables in InvokeArgument. PiperOrigin-RevId: 611467660 Change-Id: Ic89ffc986141bee61f835cb60088aee92eb8bad9 --- googlemock/include/gmock/gmock-more-actions.h | 5 +++-- googlemock/test/gmock-more-actions_test.cc | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/googlemock/include/gmock/gmock-more-actions.h b/googlemock/include/gmock/gmock-more-actions.h index dd90e31..e341d47 100644 --- a/googlemock/include/gmock/gmock-more-actions.h +++ b/googlemock/include/gmock/gmock-more-actions.h @@ -592,8 +592,9 @@ namespace internal { // Overloads for other custom-callables are provided in the // internal/custom/gmock-generated-actions.h header. template -auto InvokeArgument(F f, Args... args) -> decltype(f(args...)) { - return f(args...); +auto InvokeArgument(F &&f, + Args... args) -> decltype(std::forward(f)(args...)) { + return std::forward(f)(args...); } template diff --git a/googlemock/test/gmock-more-actions_test.cc b/googlemock/test/gmock-more-actions_test.cc index 16af689..7ed89a9 100644 --- a/googlemock/test/gmock-more-actions_test.cc +++ b/googlemock/test/gmock-more-actions_test.cc @@ -91,6 +91,10 @@ struct UnaryMoveOnlyFunctor : UnaryFunctor { UnaryMoveOnlyFunctor(UnaryMoveOnlyFunctor&&) = default; }; +struct OneShotUnaryFunctor { + int operator()(bool x) && { return x ? 1 : -1; } +}; + const char* Binary(const char* input, short n) { return input + n; } // NOLINT int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT @@ -716,6 +720,12 @@ TEST(InvokeArgumentTest, Functor1MoveOnly) { EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryMoveOnlyFunctor()))); } +// Tests using InvokeArgument with a one-shot unary functor. +TEST(InvokeArgumentTest, OneShotFunctor1) { + Action a = InvokeArgument<0>(true); // NOLINT + EXPECT_EQ(1, a.Perform(std::make_tuple(OneShotUnaryFunctor()))); +} + // Tests using InvokeArgument with a 5-ary function. TEST(InvokeArgumentTest, Function5) { Action a = // NOLINT -- cgit v0.12