Set up inlining for InvokeWithoutArgs when passed a functor.

Actions can now be implicitly constructed from zero-argument callables. There is no need to create wrapper objects using InvokeWithoutArgs().

PiperOrigin-RevId: 949656497
Change-Id: Ida2bc38e446e7b33aaf185ec593caf2a1959138e
This commit is contained in:
Abseil Team
2026-07-17 10:57:56 -07:00
committed by Copybara-Service
parent 4141c384aa
commit fa005b296f
3 changed files with 10 additions and 25 deletions

View File

@@ -1331,22 +1331,6 @@ struct InvokeMethodAction {
}
};
// Implements the InvokeWithoutArgs(f) action. The template argument
// FunctionImpl is the implementation type of f, which can be either a
// function pointer or a functor. InvokeWithoutArgs(f) can be used as an
// Action<F> as long as f's type is compatible with F.
template <typename FunctionImpl>
struct InvokeWithoutArgsAction {
FunctionImpl function_impl;
// Allows InvokeWithoutArgs(f) to be used as any action whose type is
// compatible with f.
template <typename... Args>
auto operator()(const Args&...) -> decltype(function_impl()) {
return function_impl();
}
};
// Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action.
template <class Class, typename MethodPtr>
struct InvokeMethodWithoutArgsAction {
@@ -2070,9 +2054,11 @@ internal::InvokeMethodAction<Class, MethodPtr> Invoke(Class* obj_ptr,
// Creates an action that invokes 'function_impl' with no argument.
template <typename FunctionImpl>
internal::InvokeWithoutArgsAction<typename std::decay<FunctionImpl>::type>
InvokeWithoutArgs(FunctionImpl function_impl) {
return {std::move(function_impl)};
GTEST_INTERNAL_DEPRECATE_AND_INLINE(
"Actions can now be implicitly constructed from zero-argument callables. "
"No need to create wrapper objects using InvokeWithoutArgs().")
std::decay_t<FunctionImpl> InvokeWithoutArgs(FunctionImpl&& function_impl) {
return std::forward<FunctionImpl>(function_impl);
}
// Creates an action that invokes the given method on the given object

View File

@@ -1223,15 +1223,15 @@ class Foo {
// Tests InvokeWithoutArgs(function).
TEST(InvokeWithoutArgsTest, Function) {
// As an action that takes one argument.
Action<int(int)> a = InvokeWithoutArgs(Nullary); // NOLINT
Action<int(int)> a = Nullary; // NOLINT
EXPECT_EQ(1, a.Perform(std::make_tuple(2)));
// As an action that takes two arguments.
Action<int(int, double)> a2 = InvokeWithoutArgs(Nullary); // NOLINT
Action<int(int, double)> a2 = Nullary; // NOLINT
EXPECT_EQ(1, a2.Perform(std::make_tuple(2, 3.5)));
// As an action that returns void.
Action<void(int)> a3 = InvokeWithoutArgs(VoidNullary); // NOLINT
Action<void(int)> a3 = VoidNullary; // NOLINT
g_done = false;
a3.Perform(std::make_tuple(1));
EXPECT_TRUE(g_done);

View File

@@ -1026,9 +1026,8 @@ TEST(DoAllTest, NoArgs) {
TEST(DoAllTest, MoveOnlyArgs) {
bool ran_first = false;
Action<int(std::unique_ptr<int>)> a =
DoAll(InvokeWithoutArgs([&] { ran_first = true; }),
[](std::unique_ptr<int> p) { return *p; });
Action<int(std::unique_ptr<int>)> a = DoAll(
[&] { ran_first = true; }, [](std::unique_ptr<int> p) { return *p; });
EXPECT_EQ(7, a.Perform(std::make_tuple(std::unique_ptr<int>(new int(7)))));
EXPECT_TRUE(ran_first);
}