C++ 将boost::bind作为参数的函数的签名(boost::asio计时器回调)?

C++ 将boost::bind作为参数的函数的签名(boost::asio计时器回调)?,c++,boost,boost-asio,C++,Boost,Boost Asio,我正在尝试编写一个包装器方法set_timer(),它需要一个时间和回调(使用boost::bind表示),并设置一个运行回调的截止时间计时器 目前回调非常简单: void foo::callback(const boost::system::error_code& error) { std::cout << "callback" << std::endl; } 编译时的错误消息为: foo.cpp: In member function ‘void f

我正在尝试编写一个包装器方法set_timer(),它需要一个时间和回调(使用boost::bind表示),并设置一个运行回调的截止时间计时器

目前回调非常简单:

void foo::callback(const boost::system::error_code& error) {
    std::cout << "callback" << std::endl;
}
编译时的错误消息为:

foo.cpp: In member function ‘void foo::run()’:
foo.cpp:50: error: no matching function for call to ‘foo::set_timer(boost::posix_time::seconds, boost::_bi::bind_t<void (&)(const boost::system::error_code&), boost::_mfi::dm<void(const boost::system::error_code&), foo>, boost::_bi::list1<boost::_bi::value<foo*> > >)’
src/foo_sdr.cpp:43: note: candidates are: void foo::set_timer(boost::posix_time::ptime, boost::function<void(const boost::system::error_code&)>)
foo.cpp:在成员函数“void foo::run()”中:
foo.cpp:50:错误:调用“foo::set_timer(boost::posix_time::seconds,boost:_bi::bind_t)”时没有匹配的函数
src/foo_sdr.cpp:43:注意:候选项是:void foo::set_timer(boost::posix_time::ptime,boost::function)
看起来签名要比我想象的复杂得多或者简单得多

我想用如下内容替换回调签名:

typedef std::function<void(const boost::system::error_code&)> timer_callback_t;
typedef std::函数计时器\u回调\u t;
我在CentOS 6.6上使用gcc 4.4.7和-std=gnu++0x,因此无法访问
auto


我不知道我的处境有什么不同。

实际上,
boost::bind
的返回类型是一个表达式模板,比函数类型复杂得多。毕竟,它不是一个函数,而是一个动态合成类型的函数对象(包含绑定属性值)

这种类型是实现定义的,只要确保生成的签名与您试图分配给的签名一致,您就不需要知道它。实际上,处理程序需要
error\u code
类型的单个未绑定参数,因此请按照注释中的建议添加占位符:

boost::bind (&foo::callback, this, ai::placeholders::error)
一些常规修复,以及使其自包含的更多步骤:

  • time\u duration
    不能转换为
    ptime
    。也许您想说的是从现在开始的
    expires\u
    ,而不是在处的
    expires\u。但如果您确实想要后者,请指定一个绝对截止日期:

    posix_time::microsec_clock::universal_time() + posix_time::seconds(1)
    

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

namespace ai = boost::asio;

struct foo {

    ai::io_service svc;
    ai::deadline_timer t{svc};

    void callback(const boost::system::error_code& error) {
        std::cout << "callback: " << error.message() << std::endl;
    }

    void set_timer(boost::posix_time::ptime time, boost::function<void(const boost::system::error_code&)> handler) {
        t.expires_at(time);
        t.async_wait(handler);
    }

    void fornow() {
        set_timer(
                boost::posix_time::microsec_clock::universal_time() + boost::posix_time::seconds(1),
                boost::bind (&foo::callback, this, ai::placeholders::error)
            );

        svc.run();
    }
};

int main() {
    foo o;
    o.fornow();
}

您需要一个占位符(_1)作为boost::bind中回调方法的参数。您是否考虑
设置_计时器(boost::posix_time::seconds(1),boost::bind(&foo::callback,this,_1))?那可不行。我认为签名中需要有
boost::asio::placeholders::error
。当然,您是正确的。执行
set_timer(boost::posix_time::seconds(1),boost::bind(&foo::callback,this,boost::asio::placeholders::error))会导致类似的错误消息。上面的编译器错误告诉我,我正在使用
boost:bind()
调用创建一个比
set\u timer()
中的接收参数更复杂的签名。我遗漏了一些重要的概念,这很有效!我确信不匹配是由于我没有仔细检查我的时间类型。
posix_time::microsec_clock::universal_time() + posix_time::seconds(1)
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>

namespace ai = boost::asio;

struct foo {

    ai::io_service svc;
    ai::deadline_timer t{svc};

    void callback(const boost::system::error_code& error) {
        std::cout << "callback: " << error.message() << std::endl;
    }

    void set_timer(boost::posix_time::ptime time, boost::function<void(const boost::system::error_code&)> handler) {
        t.expires_at(time);
        t.async_wait(handler);
    }

    void fornow() {
        set_timer(
                boost::posix_time::microsec_clock::universal_time() + boost::posix_time::seconds(1),
                boost::bind (&foo::callback, this, ai::placeholders::error)
            );

        svc.run();
    }
};

int main() {
    foo o;
    o.fornow();
}
callback: Success