简体   繁体   English

如何将非静态成员函数指针传递给 C++ 中的模板函数

[英]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 .这似乎只适用于不是classstruct成员函数的函数指针。 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.

相关问题 如何将C ++函数指针(非静态成员函数)传递给预定义的C函数? - How to pass in a C++ function pointer (of non-static member function) to a pre-defined C Function? C++:如何返回指向非静态成员 function 的指针? - C++: How to return a pointer to a non-static member function? 指向模板类的非静态成员函数的C ++函数指针(类成员) - C++ function pointer (class member) to non-static member function of a template class C ++将指针传递给非静态成员函数 - C++ Passing pointer to non-static member function 使用模板的非静态成员函数的C ++指针 - C++ Pointer to Non-static Member Function Using Templates 如何将非静态成员函数传递给模板参数? - how to pass non-static member function to template argument? 将函数指针传递给非静态成员函数 - pass function-pointer to non-static member-function C ++函数指针(类成员)到非静态成员函数 - C++ function pointer (class member) to non-static member function C ++函数接受函数指针和非静态成员函数作为参数 - C++ function accepting function pointer and non-static member function as parameter Invalid use of a non-static member function - How to pass a function pointer of a member function to a non-member function? - Invalid use of a non-static member function - How to pass a function pointer of a member function to a non-member function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM