boost::function 参数的默认值?

发布于 2024-08-22 20:02:01 字数 310 浏览 3 评论 0原文

我有一个函数,我想采用可选的 boost::function 参数作为报告错误情况的回调。是否有一些特殊值我可以使用默认值使其可选?

例如,使用常规函数指针,我可以执行以下操作:

void my_func(int a, int b, t_func_ptr err_callback=NULL) {

   if (error && (err_callback != NULL))
      err_callback();

}

我可以使用 boost::function 替换函数指针来执行类似的操作吗?

I've got a function that I want to take an optional boost::function argument as a callback for reporting an error condition. Is there some special value I can use a the default value to make it optional?

For example, with a regular function pointer I can do:

void my_func(int a, int b, t_func_ptr err_callback=NULL) {

   if (error && (err_callback != NULL))
      err_callback();

}

Can I do something similar with boost::function replacing the function pointer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所有深爱都是秘密 2024-08-29 20:02:01

您可以使用 0 (C++ 中的 NULL 等效项)作为 boost::function 参数的默认值,这将生成一个空函数对象。您可以通过调用其 empty() 方法、将其与 0 进行比较或简单地在布尔上下文中使用它来测试它是否为空:

void my_func(int a, int b, boost::function<void()> err_callback = 0) {
   if (error && err_callback)  // tests err_callback by converting to bool
      err_callback();
}

boost::function 对象几乎可以工作在这方面就像普通的函数指针一样。

You can use 0 (C++'s equivalent of NULL) for the default value of a boost::function argument, which will result to an empty function object. You can test if it's empty by calling its empty() method, comparing it to 0, or simply using it in a boolean context:

void my_func(int a, int b, boost::function<void()> err_callback = 0) {
   if (error && err_callback)  // tests err_callback by converting to bool
      err_callback();
}

boost::function objects work pretty much like plain function pointers in this respect.

凉薄对峙 2024-08-29 20:02:01

一个好的特殊值可以是默认构造的 boost::function

空的 function 在布尔上下文(如 NULL 指针)中计算 false,或者您可以使用 empty() 方法来测试函数对象包装器是否实际上包含函数。请参阅 boost::function 教程

这是一个代码示例:

#include <boost/function.hpp>
#include <iostream>

typedef boost::function<void (int code)> t_err_callback;

void do_callback(int code)
{
    std::cout << "Error " << code << std::endl;
}

void my_func(int a, int b, t_err_callback err_callback=t_err_callback())
{
    bool error = true;   // An error happened
    int error_code = 15; // Error code
    if (error && !err_callback.empty())
        err_callback(error_code);
}

int main()
{
    my_func(0, 0);
    my_func(0, 0, do_callback);
}

A good special value could be a default-constructed boost::function.

An empty function evaluates false in a boolean context (like a NULL pointer), or you can use the empty() method to test if a function object wrapper actually contains a function. See the boost::function Tutorial.

Here is a code sample:

#include <boost/function.hpp>
#include <iostream>

typedef boost::function<void (int code)> t_err_callback;

void do_callback(int code)
{
    std::cout << "Error " << code << std::endl;
}

void my_func(int a, int b, t_err_callback err_callback=t_err_callback())
{
    bool error = true;   // An error happened
    int error_code = 15; // Error code
    if (error && !err_callback.empty())
        err_callback(error_code);
}

int main()
{
    my_func(0, 0);
    my_func(0, 0, do_callback);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文