Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::bind如何定义以回调函数为参数的函数_C++_Boost Asio - Fatal编程技术网

C++ boost::bind如何定义以回调函数为参数的函数

C++ boost::bind如何定义以回调函数为参数的函数,c++,boost-asio,C++,Boost Asio,我正在尝试编写一个调用boost::asio操作的库模块。该模块将由应用程序逻辑层根据结果进行进一步处理 我的要求简单明了。我所需要的是能够在下面的Util类中指定回调函数fn,它可以接受任何函数ptr、boost::bind(member fn)或其他任何函数,并在异步代码完成后调用该回调函数 class Util { public: void sendMsg(const Msg& m, <some callback fn here...>) {

我正在尝试编写一个调用boost::asio操作的库模块。该模块将由应用程序逻辑层根据结果进行进一步处理

我的要求简单明了。我所需要的是能够在下面的Util类中指定回调函数fn,它可以接受任何函数ptr、boost::bind(member fn)或其他任何函数,并在异步代码完成后调用该回调函数

class Util {
  public:
    void sendMsg(const Msg& m, <some callback fn here...>) {
       // Make a call to boost::asio approximately as below..
       boost::asio::async_write(socket_, boost::asio::buffer(message_),
                   boost::bind(&tcp_connection::handle_write, shared_from_this(),
                   boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));

      // Here, I want to pass the "<some callback fn here>" to async_write,
      // instead of calling boost::bind here.  How to do that?
    }
};
class-Util{
公众:
void sendMsg(const Msg&m){
//调用boost::asio,大致如下。。
boost::asio::异步写入(套接字),boost::asio::缓冲区(消息),
boost::bind(&tcp_connection::handle_write,shared_from_this(),
boost::asio::占位符::错误,
boost::asio::占位符::字节(已传输);
//这里,我想将“”传递给async_write,
//而不是在这里调用boost::bind。怎么做?
}
};
所以,我的具体问题是。。 我的sendMsg签名应该是什么样子?我无法获得fn签名,尤其是回调部分。


感谢所有人让我知道如何做到这一点。

如果您可以访问lambdas,那就很容易了

class Util {
  public:
    template <typename _Proc>
    void sendMsg(const Msg& m, _Proc proc) 
    {
        shared_ptr<Util> pThis = shared_from_this;
        boost::asio::async_write(socket_, boost::asio::buffer(message_),
            [&proc, =pThis/* , etra args */](boost::...::error_code ec, size_t bytes) // <- use capture to pass extra agruments and shared ptr
            {
               // use pThis !
               proc(/* etra args */);
               // or pthis->proc()
            });
        }

};
class-Util{
公众:
模板
void sendMsg(const Msg&m,_procproc)
{
shared_ptr pThis=来自此的共享;
boost::asio::异步写入(套接字),boost::asio::缓冲区(消息),
[&proc,=pThis/*,etra args*/](boost:::::错误代码ec,大小字节)//proc()
});
}
};

这将接受lambda以及std/boost::function

std::function
?还是模板类型?好的。最近我和C++的很多东西失去联系了。你能提供一个简单的例子吗。。谢谢,谢谢你的提示。我好像明白了。我有点老学校的C++。熟悉boost,async的东西。谢谢。但是对于Util类,Y共享了\u ptr?Util::sendMsg的调用方应该提供回调函数fn,我需要将其传递给async_write。否?通过对bind()的调用或lambda capture,将共享的_ptr按值传递给调用对象。它的唯一目的是在i/o操作或计时器挂起时保持对象的活动状态
将在与客户端的对话结束时自动销毁,除非您将其保存在其他地方。通常,会传递消息智能指针。