【发布时间】:2011-09-02 11:40:55
【问题描述】:
m_io_service.post(boost::ref(i));
我在一段代码中有这个调用,底层类型 i 绝对是一个可调用的(因为删除 boost::ref 会导致按值传递,这很好用),但是 clang 告诉我:
/opt/dev_64_swat/proto-rpc2/dependencies/boost/include/boost/asio/handler_invoke_hook.hpp:64:3: error: type 'boost::reference_wrapper<rubble::rpc::TcpFrontEndConnectionInvoker>' does not provide a call operator
我如何通过引用传递,我的对象比异步调用更长寿,如果我可以通过引用传递它们,它们会更优雅(更少 boost::shared_ptr<..> 的成员)。
-- 编辑--
我已经浏览了 asio 的示例目录,并且没有为完成处理程序演示 boost::ref。所以我想我在这里不走运。处理程序没有接受 ref 的版本是否有原因?
-- 编辑 2:我的样子(除非您对实现持怀疑态度,否则不要费心看这个)。 --
namespace rubble { namespace rpc {
struct InProcessInvoker : public InvokerBase
{
struct notification_object_
{
typedef notification_object_ * ptr;
notification_object_()
{
reset();
}
void reset()
{
ready = false;
}
bool ready;
boost::mutex mutex;
boost::condition_variable cond;
};
InProcessInvoker(BackEnd & b_in)
: b(b_in),
notification_object(new notification_object_())
{
b.connect(m_client_data);
}
~InProcessInvoker()
{
if( m_client_data.unique() )
{
b.disconect(m_client_data);
delete notification_object;
}
}
bool is_useable()
{
return b.is_useable();
}
void reset()
{
notification_object->reset();
m_client_data->request().Clear();
m_client_data->response().Clear();
m_client_data->error_code().clear();
BOOST_ASSERT_MSG( m_client_data->is_rpc_active() == false,
"THE FLAG THAT REPRESENTS ACTIVE "
"RPC SHOULD NOT BE SET WHEN RESETING AN OBJECT FOR RPC");
}
void invoke()
{
b.invoke(*this);
}
void operator() ()
{
service->dispatch(*client_cookie,*m_client_data);
b.end_rpc(m_client_data.get());
boost::lock_guard<boost::mutex> lock(notification_object->mutex);
notification_object->ready=true;
notification_object->cond.notify_one();
}
void after_post()
{
boost::unique_lock<boost::mutex> lock(notification_object->mutex);
if(!notification_object->ready)
notification_object->cond.wait(lock);
}
notification_object_::ptr notification_object;
BackEnd & b;
};
} }
【问题讨论】:
-
你能展示一下
i的样子吗? -
你要求的 :D 基类有三个 ptr 和访问器。
-
我实际上是指定义。 :)
i需要是一个处理程序,boost::ref只给你一个没有operator()的包装器。
标签: c++ functional-programming pass-by-reference boost-asio boost-ref