C++ 在boost::deadline_计时器上创建包装器,无法将参数传递给处理程序函数

C++ 在boost::deadline_计时器上创建包装器,无法将参数传递给处理程序函数,c++,boost,boost-asio,C++,Boost,Boost Asio,我试图创建一个简单的BoostTimer类,它封装了deadline\u timer的基本函数,如async\u wait和cancel,这样我的程序就可以调用startTimer和killTimer,我已经编写了以下代码 boosttimer.h timertest.cpp #包括 #包括 #包括“boosttimer.h” //无效计时器\u处理程序(const boost::system::error\u code&/*e*/)//无法获取参数 void timer\u handler()

我试图创建一个简单的
BoostTimer
类,它封装了
deadline\u timer
的基本函数,如
async\u wait
cancel
,这样我的程序就可以调用
startTimer
killTimer
,我已经编写了以下代码

boosttimer.h timertest.cpp
#包括
#包括
#包括“boosttimer.h”
//无效计时器\u处理程序(const boost::system::error\u code&/*e*/)//无法获取参数
void timer\u handler()//在没有参数的情况下运行正常
{

std::cout如果只想从调用方传递参数:

void timer_handler(std::string const& arg1, int arg2)
{
    std::cout<<"timer function has been called with arg1='" << arg1 <<"', arg2=" << arg2 << std::endl;
}

int main()
{

    boost::asio::io_service io_service;
    BoostTimer timer(io_service,boost::posix_time::seconds(5), boost::bind(&timer_handler, "This is arg1", 42));
    timer.startTimer();

    io_service.run();
    return 0;
}
要同时通过
ec
*此

<强> CaveS/<强>我认为这严重地破坏了任何封装,使整个类基本上是多余的。在取消的情况下,不要调用完成处理程序,也可以让调用方绑定它需要的对象实例。(对于用户定义的处理程序来说,要求引用

BoostTimer
,这是一种奇怪的做法——这是在错误的方向上紧密耦合)


当我在将
timer\u处理程序
绑定到
BoostTimer
时传递2个参数时,它可以正常工作,但是如果我只想pas
ec
*这个
(例如
BoostTimer
),当我在创建
BoostTimer
时从
timer\u handler
\u 1
\u 2
中删除参数
arg1
arg2
时,它开始给出编译错误,有任何错误吗?@Tejendra你一定做错了什么哦,是的,我没有将_1和_2作为参数传递,这就解释了,谢谢伙计,顺便说一句,我会关注你的频道,这真的很有帮助。我更愿意把
std::bind(callable,_1,_2)
看作是绑定两个占位符,而不是传递(因为这只是创建绑定可调用的表达式模板。从概念上讲,你没有传递
\u 1
\u 2
)因此,答案表明,您可以将值或占位符绑定到n元可调用对象,以创建m元可调用对象(m
#include "boosttimer.h"

BoostTimer::BoostTimer(boost::asio::io_service& io_service, duration interval, handler_function handler) :
        _timer(io_service),
        _ioService(io_service),
        _interval(interval),
        _handler(handler)
{ 
}

BoostTimer::~BoostTimer()
{
}

void BoostTimer::startTimer()
{
        _timer.expires_from_now(_interval);
        _timer.async_wait(boost::bind(_handler, boost::asio::placeholders::error, boost::ref(*this))); //trying to pass placeholder argument but somehow it doesn't work
}

void BoostTimer::killTimer()
{
        _timer.cancel();
}
#include <iostream>
#include <boost/asio.hpp>
#include "boosttimer.h"

//void timer_handler(const boost::system::error_code& /*e*/) // not able to take parameters
void timer_handler() //it runs fine without parameters 
{
        std::cout<<"timer function has been called" << std::endl;
}

int main(int argc, char* argv[])
{

    boost::asio::io_service io_service;
    BoostTimer timer(io_service,boost::posix_time::seconds(5), boost::bind(&timer_handler));
    timer.startTimer();

    io_service.run();
    return 0;
}
void timer_handler(std::string const& arg1, int arg2)
{
    std::cout<<"timer function has been called with arg1='" << arg1 <<"', arg2=" << arg2 << std::endl;
}

int main()
{

    boost::asio::io_service io_service;
    BoostTimer timer(io_service,boost::posix_time::seconds(5), boost::bind(&timer_handler, "This is arg1", 42));
    timer.startTimer();

    io_service.run();
    return 0;
}
timer function has been called with arg1='This is arg1', arg2=42
void timer_handler(boost::system::error_code ec, BoostTimer& instance, std::string const& arg1, int arg2) //it runs fine without parameters 
{
    std::cout<<"timer function has been called with arg1='" << arg1 <<"', arg2=" << arg2 << " (" << ec.message() << ")\n";
}

int main()
{
    boost::asio::io_service io_service;
    BoostTimer timer(io_service,boost::posix_time::seconds(1), boost::bind(&timer_handler, _1, _2, "This is arg1", 42));
    timer.startTimer();

    io_service.run();
    return 0;
}
timer function has been called with arg1='This is arg1', arg2=42 (Success)