c++ - Boost 与 asio::placeholders::error 绑定(bind)

标签 c++ function boost bind boost-asio

为什么它不起作用?

--- boost_bind.cc ---

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

void func1 (const int& i)
{ }

void func2 (const ::asio::error_code& e)
{ }

int main ()
{
    ::boost::function<void()> f1 = ::boost::bind (&func1, 1);

    // it doesn't work!
    ::boost::function<void()> f2 = ::boost::bind (&func2, ::asio::placeholders::error);

    return 0;
}

这是错误:

while_true@localhost:~> g++ -lpthread boost_bind.cc -o boost_bind
In file included from boost_bind.cc:2:
/usr/include/boost/bind.hpp: In member function ‘void boost::_bi::list1::operator()(boost::_bi::type, F&, A&, int) [with F = void (*)(const asio::error_code&), A = boost::_bi::list0, A1 = boost::arg (*)()]’:
/usr/include/boost/bind/bind_template.hpp:20:   instantiated from ‘typename boost::_bi::result_traits::type boost::_bi::bind_t::operator()() [with R = void, F = void (*)(const asio::error_code&), L = boost::_bi::list1 (*)()>]’
/usr/include/boost/function/function_template.hpp:152:   instantiated from ‘static void boost::detail::function::void_function_obj_invoker0::invoke(boost::detail::function::function_buffer&) [with FunctionObj = boost::_bi::bind_t (*)()> >, R = void]’
/usr/include/boost/function/function_template.hpp:904:   instantiated from ‘void boost::function0::assign_to(Functor) [with Functor = boost::_bi::bind_t (*)()> >, R = void]’
/usr/include/boost/function/function_template.hpp:720:   instantiated from ‘boost::function0::function0(Functor, typename boost::enable_if_c::type) [with Functor = boost::_bi::bind_t (*)()> >, R = void]’
/usr/include/boost/function/function_template.hpp:1040:   instantiated from ‘boost::function::function(Functor, typename boost::enable_if_c::type) [with Functor = boost::_bi::bind_t (*)()> >, R = void]’
boost_bind.cc:14:   instantiated from here
/usr/include/boost/bind.hpp:232: error: no match for ‘operator[]’ in ‘a[boost::_bi::storage1 (*)()>::a1_ [with int I = 1]]’
while_true@localhost:~>

最佳答案

我认为你应该写 boost::asio::placeholders::error 而不是 ::asio::placeholders::error。另外,我不明白你在 boost 命名空间前面的 :: 的用途。

关于c++ - Boost 与 asio::placeholders::error 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2717721/

相关文章:

c++ - 多重继承中使用operator delete时谁来调用类的Destructor

c++ - 在同一个类的每个对象上调用方法

C++使用if-else语句计算sin和cos

c++ - 函数的前向声明搞砸了,说 args 是未声明的标识符

具有删除旧样本能力的 C++ 累加器库

java - 如何编译由 Java 或 C++ 应用程序生成的代码

c++ - 返回与不返回函数?

c - C函数中的多维char数组

c++ - 如何循环进入同名节点

c++ - 使用 Boost Graph Library 将 boost 动态属性写入文件