具有变量计数和类型参数的函数指针?

Function-pointer with variable count and type parameter?

在 C++ 中,我需要将一些变量传递给一个函数,并且我希望该函数使用我的参数回调我的函数指针:

// Example program
#include <iostream>
#include <string>

using namespace std;

typedef void(*callback)(int,int);

void otherFunction(string query, callback c, ??) {
    cout << "otherFunction is processing data" << endl;
    c(??, queryResult);
}

void callbackAfterOtherFunction(int queryId, int result) {
    cout << "Calculation finished: value=" << value << ", value2=" << value2 << ", result=" << result << endl;
}

void doSomething(string query, int queryId) {
  otherFunction(query, callbackAfterOtherFunction, queryId);       
}

int main()
{
  doSomething("QUERY", 1); 
  return 0;
}

这段代码有效,但我觉得它很难看,因为我必须为我的回调函数定义参数列表int,int,int

我可以使用 C++ 中的任何方法来简化它吗?即:otherFunction只会得到一个函数指针和一些参数,它会用提供的参数调用该函数+它的单个int计算结果.

注意:callbackAfterOtherFunction 是线程安全的,因为 otherFunction 可能会从不同的线程回调它。

示例: 考虑一个 DBManager class,它可以异步方式从 DB 查询数据,因此它定义了 otherFunction 接受查询(在本例中为 query),以及它将调用的函数指针查询数据后返回。但是我作为 DBManager 是异步的,我可以在开始取回结果之前多次调用 otherFunction 。所以我想在otherFunction中添加参数来标记我的查询,这样当回调返回时,可以区分数据。

Berto99的解决方案:

// Example program
#include <iostream>
#include <string>

using namespace std;

template<class Callback, class ...T>
void otherFunction(Callback c, T ...params) {
    cout << "otherFunction is processing data" << endl;
    int res=14;
    //c(params..., (params + ...));
}

void callbackAfterOtherFunction(int value, int value2, int result) {
    cout << "Calculation finished: value=" << value << ", value2=" << value2 << ", result=" << result << endl;
}

void doSomething(int value, int value2) {
    cout << "Processing values: Value=" << value << ", value2=" << value2 << endl;
    otherFunction(callbackAfterOtherFunction, value, value2);
}

int main()
{
    otherFunction(doSomething,2,3);
    return 0;
}

这里的预期结果是

Calculation finished: value=2, value2=3, result=14

最简单的解决方案是使用这样的模板:

// Example program
#include <iostream>
#include <string>

using namespace std;

template<typename C>
void otherFunction(int value, int value2, C c) {
    cout << "otherFunction is processing data" << endl;
    c(value, value2, value+value2);
}

void callbackAfterOtherFunction(int value, int value2, int result) {
    cout << "Calculation finished: value=" << value << ", value2=" << value2 << ", result=" << result << endl;
}

void doSomething(int value, int value2) {
  cout << "Processing values: Value=" << value << ", value2=" << value2 << endl;
  otherFunction(value, value2, callbackAfterOtherFunction);       
}

int main()
{
  doSomething(2,3); 
  return 0;
}

我建议使用 variadic template 作为参数,template argument deduction 以避免声明函数的类型:

template<class Callback, class ...T>
void otherFunction(Callback c, T ...params) {
    cout << "otherFunction is processing data" << endl;
    c(params...); // if you want to have the last parameter to be the sum of all the parameters: c(params..., (params + ...));
}

整个代码应该是这样的:


template<class Callback, class ...T>
void otherFunction(Callback c, T ...params) {
    cout << "otherFunction is processing data" << endl;
    c(params...);
}

void callbackAfterOtherFunction(int value, int value2, int result) {
    cout << "Calculation finished: value=" << value << ", value2=" << value2 << ", result=" << result << endl;
}

void doSomething(int value, int value2) {
    cout << "Processing values: Value=" << value << ", value2=" << value2 << endl;
    //otherFunction(value, value2, callbackAfterOtherFunction);
}

int main()
{
    otherFunction(doSomething,2,3);
    return 0;
}

输出:

otherFunction is processing data
Processing values: Value=2, value2=3

编辑:
如果你需要参数的总和,感谢cdhowie,你可以使用之前的代码并像这样调用回调:

c(params..., (params + ...));

代码


template<class Callback, class ...T>
void otherFunction(Callback c, T ...params) {
    cout << "otherFunction is processing data" << endl;
    int res=14;
    c(params..., (params + ...));
}

void callbackAfterOtherFunction(int value, int value2, int result) {
    cout << "Calculation finished: value=" << value << ", value2=" << value2 << ", result=" << result << endl;
}

template<class ...T>
void doSomething(T ... els) {
    cout << "Processing values";
    otherFunction(callbackAfterOtherFunction, els...);
}

int main()
{
    doSomething(2,3);
    return 0;
}

这将允许您计算任意数量的参数:

int a = 2;
int b = 3;
anotherFunction(acallbackAfterOtherFunction, doSomething,a,b,4,5,6);
using namespace std;

template<class Callback, typename T>
int otherFunction(Callback c, T one) {
    cout << "otherFunction is processing ONE data " << one << endl;
    return one;
}

template<class Callback, typename A, typename... T>
int otherFunction(Callback c, A one, T... params) {
    cout << "otherFunction is processing data" << endl;
    
    return c(otherFunction(c, one), otherFunction(c, params...));
}

template<typename P, class Callback, typename A, typename... T>
int anotherFunction(P p, Callback c, A one, T... params) {
    cout << "anotherFunction is processing data" << endl;
    int i = c(otherFunction(c, one), otherFunction(c, params...));
    p (i);
    return i;
}

void callbackAfterOtherFunction(int value, int value2, int result) {
    cout << "Calculation finished: value=" << value << ", value2=" << value2 << ", result=" << result << endl;
}

void acallbackAfterOtherFunction(int result) {
    cout << "Calculation finished: result=" << result << endl;
}

int doSomething(int value, int value2) {
    cout << "Processing values: Value=" << value << ", value2=" << value2 << endl;
    return value + value2;
    //otherFunction(value, value2, callbackAfterOtherFunction);
}

int main()
{
    int a = 2;
    int b = 3;
    int r = anotherFunction(acallbackAfterOtherFunction, doSomething,a,b,4,5,6);
    cout << "R: " << r << endl;
    return 0;
}