[英]How to pass non-static member function pointer to a template function in C++
I'm trying to make a function template in a header file that accepts a generic function pointer and packed arguments.我正在尝试在接受通用函数指针和打包参数的头文件中创建一个函数模板。 The function template would invoke the received function pointer using the packed arguments.
函数模板将使用打包参数调用接收到的函数指针。 My goal is to calculate and return the execution time of the function pointer.
我的目标是计算并返回函数指针的执行时间。
#ifndef LOGTIME_HPP
#define LOGTIME_HPP
#include <chrono>
#include <ratio>
template<typename Function, typename... Args>
double getExecutionTime(Function&& function, Args&&... args) {
auto t1 = std::chrono::high_resolution_clock::now();
std::invoke(std::forward<Function>(function), std::forward<Args>(args)...);
auto t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> ms = t2 - t1;
return ms.count();
}
#endif
This seems to only work for function pointers that aren't member functions of a class
or struct
.这似乎只适用于不是
class
或struct
成员函数的函数指针。 This is some example code using the function template:这是使用函数模板的一些示例代码:
#include <iostream>
#include <thread>
#include <random>
#include "LogTime.hpp" // header including getExecutionTime()
class Obj
{
public:
int testFunc(int dur, int num) {
std::cout << "test" << num;
for (int i = 0; i < dur; i++) {
std::cout << ".";
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return 2;
}
};
int testFunc(int dur, int num) {
std::cout << "test" << num;
for (int i = 0; i < dur; i++) {
std::cout << ".";
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return 1;
}
int main()
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<> uniform_dist(1, 100);
Obj obj = Obj();
for (int i = 0; i < 10; i++) {
int rand = uniform_dist(rng);
std::cout << "elapsed: "
// this works
<< getExecutionTime(testFunc, uniform_dist(rng), i) << std::endl;
// this doesn't work
<< getExecutionTime(Obj::testFunc, uniform_dist(rng), i) << std::endl;
}
}
My issue is that Obj::testFunc
is failing.我的问题是
Obj::testFunc
失败了。 I know that if Obj::testFunc
was static, then the function would execute fine.我知道如果
Obj::testFunc
是静态的,那么该函数会执行得很好。 I've read that std::invoke
can invoke a member function , but I don't know how to incorporate this into the function template.我读过
std::invoke
可以调用成员函数,但我不知道如何将其合并到函数模板中。
I would appreciate any help or insight.我将不胜感激任何帮助或见解。
Thanks谢谢
Non-static member functions have an implicit parameter of the class type as the first parameter of the function, and it is that object which is mapped to the this
pointer.非静态成员函数有一个类类型的隐式参数作为函数的第一个参数,它就是映射到
this
指针的对象。 That means that you need to pass an object of the class type as the first argument after the member function pointer like这意味着您需要将类类型的对象作为成员函数指针之后的第一个参数传递,例如
<< getExecutionTime(&Obj::testFunc, obj, uniform_dist(rng), i) << std::endl;
or you can use a lambda instead like或者您可以使用 lambda 代替
<< getExecutionTime([&](){ obj.testFunc(uniform_dist(rng), i); }) << std::endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.