Skip to content

Commit e4fdb87

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Accept one-shot callables in InvokeArgument.
PiperOrigin-RevId: 611467660 Change-Id: Ic89ffc986141bee61f835cb60088aee92eb8bad9
1 parent e15c5a5 commit e4fdb87

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

googlemock/include/gmock/gmock-more-actions.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,9 @@ namespace internal {
592592
// Overloads for other custom-callables are provided in the
593593
// internal/custom/gmock-generated-actions.h header.
594594
template <typename F, typename... Args>
595-
auto InvokeArgument(F f, Args... args) -> decltype(f(args...)) {
596-
return f(args...);
595+
auto InvokeArgument(F &&f,
596+
Args... args) -> decltype(std::forward<F>(f)(args...)) {
597+
return std::forward<F>(f)(args...);
597598
}
598599

599600
template <std::size_t index, typename... Params>

googlemock/test/gmock-more-actions_test.cc

+10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ struct UnaryMoveOnlyFunctor : UnaryFunctor {
9191
UnaryMoveOnlyFunctor(UnaryMoveOnlyFunctor&&) = default;
9292
};
9393

94+
struct OneShotUnaryFunctor {
95+
int operator()(bool x) && { return x ? 1 : -1; }
96+
};
97+
9498
const char* Binary(const char* input, short n) { return input + n; } // NOLINT
9599

96100
int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
@@ -716,6 +720,12 @@ TEST(InvokeArgumentTest, Functor1MoveOnly) {
716720
EXPECT_EQ(1, a.Perform(std::make_tuple(UnaryMoveOnlyFunctor())));
717721
}
718722

723+
// Tests using InvokeArgument with a one-shot unary functor.
724+
TEST(InvokeArgumentTest, OneShotFunctor1) {
725+
Action<int(OneShotUnaryFunctor)> a = InvokeArgument<0>(true); // NOLINT
726+
EXPECT_EQ(1, a.Perform(std::make_tuple(OneShotUnaryFunctor())));
727+
}
728+
719729
// Tests using InvokeArgument with a 5-ary function.
720730
TEST(InvokeArgumentTest, Function5) {
721731
Action<int(int (*)(int, int, int, int, int))> a = // NOLINT

0 commit comments

Comments
 (0)