gpt4 book ai didi

c++ - 为什么编译器不能为非静态成员函数的无效使用生成解决方法

转载 作者:行者123 更新时间:2023-11-28 02:03:56 24 4
gpt4 key购买 nike

我需要一种方法来衡量函数的执行时间。我在 SO https://stackoverflow.com/a/21995693/3179492 上找到了这个非常好的答案.这是一个完美的解决方案。

它使用带有可变参数列表的函数指针。

这是 MCVE:

#include <algorithm>
#include <chrono>
#include <stdio.h>

class Foo
{
public:
auto foo_member( int x ) -> void { printf( "in foo_member(%d);\n", x ); }
};

class measure
{
public:
template<typename F, typename ...Args>
static typename std::chrono::microseconds::rep execution(F func, Args&&... args)
{
auto start = std::chrono::system_clock::now();
func(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast<std::chrono::microseconds>
(std::chrono::system_clock::now() - start);
return duration.count();
}
};

int main()
{
Foo foo;
int x = 1234;

// this line does not compile
printf( "execution time=%ld\n", measure::execution( foo.foo_member, x ) );
}

此代码无法编译,因为 foo_member 不是静态的。错误信息是无效使用非静态成员函数

解决问题的方法很少。对我来说,最优雅、最短、最有效的方法是这样的:

printf("execution time=%ld\n", measure::execution( [&foo, &x] () { foo.foo_member(x); } ));

这意味着,我使用 lambda 函数来编译该行。

我只是想知道为什么编译器不能为我做这件事?代码路径精确定义了如何使用捕获机制第一个版本转换为 lambda 函数。当您刚刚意识到现代 c++ 编译器正在对代码执行的操作时,这确实是最简单的代码重新排序之一...

注意:

它是使用这些 gcc 标志编译的:-Wall -Werror -Wextra -std=c++11

最佳答案

foo_member 是一个非静态成员函数,因此它有一个隐含的第一个参数,即 this 指针,您必须将其传递给它。创建指向成员函数的指针的正确语法是 &ClassName::Func,而不是 class_instance.Func

要修复您的示例,请进行以下更改:

main

printf( "execution time=%ld\n", measure::execution( &Foo::foo_member, &foo, x ) );

现在您传递一个指向 Foo::foo_member 的指针,以及一个指向 Foo 实例的指针作为第一个参数。 foo_member 将在此实例上调用。

measure::execution 需要修改以正确处理指向成员函数的指针。最简单的方法可能是使用 std::experimental::apply .

std::experimental::apply(func, std::forward_as_tuple(std::forward<Args>(args)...));

或者如果你有 C++17 编译器,你可以使用 std::invoke ,在这种情况下,您可以避免调用 forward_as_tuple

Live demo (使用 std::experimental::apply)

Live demo (使用 std::invoke)

关于c++ - 为什么编译器不能为非静态成员函数的无效使用生成解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com