【问题标题】:How to pass std::function with different parameters to same function如何将具有不同参数的 std::function 传递给同一个函数
【发布时间】:2018-08-04 16:21:48
【问题描述】:

我希望将三个函数合并在一起。

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

问题是,存在三种不同类型的函数。没有参数的函数,有一个整数参数的函数,以及有两个整数参数的函数。带整数参数的也有对应的参数通过原函数传递。

正如大家所见,每个功能都几乎相同,所以如果我能将它们全部合并在一起就好了。但是,我不确定是否要设置一个可以接收任何形式的std::function 的参数,并且还依赖于它已提供相应数据以供使用的事实。

以下是函数:

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

任何建议将不胜感激!

【问题讨论】:

  • 我感觉到一个模板函数的可能应用与可变模板参数。

标签: c++ c++11 templates variadic-templates std-function


【解决方案1】:

它似乎适用于可变参数模板。

类似:

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

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

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

【讨论】:

  • 无论如何我都会支持这个,但您可能想为此添加完美转发。只是说。
  • @liliscent - 哦!谢谢!
  • @WhozCraig - 你是对的,但我有疑问:如何为 std::function 的类型参数编写引用?
  • @Griffort - Args... 的东西是“可变参数模板”;它是 C++11 的一项基本创新(它们在 C++98 中不存在)。我不知道有什么建议去学习它们,但是如果你找到了 C++11 的好资源,你是否找到了可变参数模板的好资源。
  • @Grifford - 在你的情况下(一个只接收简单整数的函数)没有区别。更一般地说,第二种形式比第一种形式要好得多,因为允许在适当的时候激活移动语义。移动语义对于 C++11 来说是一个巨大的性能改进。
【解决方案2】:

我建议使用 lambda 函数:

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

要调用此函数,您应该:

run_callback([]{ func_without_params(); });

run_callback([&]{ func_with_1_param(a); });

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多