繁体   English   中英

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

[英]Function-pointer with variable count and type parameter?

In C++ I need to pass some variables to a function, and I want that function to call back my function pointer with my parameters:

// 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;
}

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

C++ 中有什么方法可以用来简化这个吗? 即: otherFunction只会得到一个函数指针和一些参数,它会调用 function 并提供参数+其单个 int 计算结果

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

示例:考虑一个 DBManager class 可以以异步方式从数据库中查询数据,因此它定义了接受查询的otherFunction (在本例中为query ),以及一个 function 指针,一旦查询到数据,它将回调该指针。 但是我作为 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;
}

这里的预期结果是

计算完成: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以避免声明 function 的类型:

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;
}

Output:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM