如何将具有不同参数的std :: function传递给同一函数 [英] How to pass std::function with different parameters to same function

查看:350
本文介绍了如何将具有不同参数的std :: function传递给同一函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要合并三个函数。

每个函数都需要一个 std :: function 作为第一个参数,然后在 try / catch 块中执行它。

Each one takes an std::function as the first parameter, then executes it within a try/catch block.

问题是,存在三种不同类型的函数。没有参数的函数,具有一个整数参数的函数和具有两个整数参数的函数。带有整数参数的函数还具有通过原始函数传递的相应参数。

The issue is, there are three different types of functions. Functions with no parameters, those with one integer parameter, and those with two integer parameters. The ones with integer parameters also have their corresponding parameters passed through the original function.

可以看到,每个函数都几乎相同,因此如果我可以将它们合并在一起。但是,我不确定无论如何设置一个可以接收任何形式的 std :: function 的参数,并且还依赖于已为它提供了相应数据的事实。

As one can see, each of the functions are nearly identical, so it would be nice if I could merge them all together. However, I'm unsure of anyway to setup a parameter that can receive any form of std::function and also rely on the fact it has been provided with corresponding data to use.

以下是函数:

void run_callback(std::function<void()>& func) {
    try {
        func();
    } catch(const std::exception& ex) {
        print_callback_error(ex.what());
    } catch(const std::string& ex) {
        print_callback_error(ex.c_str());
    } catch(...) {
        print_callback_error();
    }
}

void run_callback_int(std::function<void(int)>& func, int data) {
    try {
        func(data);
    } catch(const std::exception& ex) {
        print_callback_error(ex.what());
    } catch(const std::string& ex) {
        print_callback_error(ex.c_str());
    } catch(...) {
        print_callback_error();
    }
}

void run_callback_intint(std::function<void(int, int)>& func, int data1, int data2) {
    try {
        func(data1, data2);
    } catch(const std::exception& ex) {
        print_callback_error(ex.what());
    } catch(const std::string& ex) {
        print_callback_error(ex.c_str());
    } catch(...) {
        print_callback_error();
    }
}

任何建议将不胜感激!

推荐答案

它似乎可以使用可变参数模板。

It seems to work with variadic templates.

类似的东西:

template <typename ... Args>
void run_callback(std::function<void(Args...)> const & func, Args ... as) {
    try {
        func(as...);
    } catch(const std::exception& ex) {
        print_callback_error(ex.what());
    } catch(const std::string& ex) {
        print_callback_error(ex.c_str());
    } catch(...) {
        print_callback_error();
    }
}

或(也许更好地管理可能的转发)

or (maybe better to manage possible forwarding)

template <typename ... FArgs, typename ... Args>
void run_callback(std::function<void(FArgs...)> const & func,
                  Args && ... as) {
    try {
        func(std::forward<Args>(as)...);
    } catch(const std::exception& ex) {
        print_callback_error(ex.what());
    } catch(const std::string& ex) {
        print_callback_error(ex.c_str());
    } catch(...) {
        print_callback_error();
    }
}

这篇关于如何将具有不同参数的std :: function传递给同一函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆