使用 asio::placeholders::error 进行 Boost 绑定

发布于 2024-08-30 04:59:11 字数 2125 浏览 4 评论 0原文

为什么它不起作用?

--- 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:~>

Why it doesn't work?

--- 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;
}

This is the error:

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:~>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-09-06 04:59:11

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

I think you should write boost::asio::placeholders::error instead of ::asio::placeholders::error. Also, I don't understand the purpose of :: you have in front of the boost namespace.

所有深爱都是秘密 2024-09-06 04:59:11

我想你想要 boost::system::error_code:

void func2 (const boost::system::error_code& error)
{ }

还有这个:

::boost::function<void(boost::system::error_code&)> f2 = ::boost::bind (&func2, boost::asio::placeholders::error);

我发现 asio 的占位符太冗长了,所以我会写这个:

::boost::function<void(boost::system::error_code&)> f2 = ::boost::bind (&func2, _1);

I think you want boost::system::error_code:

void func2 (const boost::system::error_code& error)
{ }

and also this:

::boost::function<void(boost::system::error_code&)> f2 = ::boost::bind (&func2, boost::asio::placeholders::error);

I find asio's placeholder stuff too verbose, so I'd write this instead:

::boost::function<void(boost::system::error_code&)> f2 = ::boost::bind (&func2, _1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文