c++ - 功能性、bind1st 和 mem_fun

标签 c++ boost functional-programming

为什么不能编译?

#include <functional> 
#include <boost/function.hpp> 

class A { 
    A() { 
        typedef boost::function<void ()> FunctionCall; 
        FunctionCall f = std::bind1st(std::mem_fun(&A::process), this); 
    } 
    void process() {} 
};

错误:

In file included from /opt/local/include/gcc44/c++/bits/stl_function.h:712,
                 from /opt/local/include/gcc44/c++/functional:50,
                 from a.cc:1:
/opt/local/include/gcc44/c++/backward/binders.h: In instantiation of 'std::binder1st<std::mem_fun_t<void, A> >':
a.cc:7:   instantiated from here
/opt/local/include/gcc44/c++/backward/binders.h:100: error: no type named 'second_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:103: error: no type named 'first_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:106: error: no type named 'first_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:111: error: no type named 'second_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:117: error: no type named 'second_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h: In function 'std::binder1st<_Operation> std::bind1st(const _Operation&, const _Tp&) [with _Operation = std::mem_fun_t<void, A>, _Tp = A*]':
a.cc:7:   instantiated from here
/opt/local/include/gcc44/c++/backward/binders.h:126: error: no type named 'first_argument_type' in 'class std::mem_fun_t<void, A>'
In file included from /opt/local/include/boost/function/detail/maybe_include.hpp:13,
                 from /opt/local/include/boost/function/detail/function_iterate.hpp:14,
                 from /opt/local/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47,
                 from /opt/local/include/boost/function.hpp:64,
                 from a.cc:2:
/opt/local/include/boost/function/function_template.hpp: In static member function 'static void boost::detail::function::void_function_obj_invoker0<FunctionObj, R>::invoke(boost::detail::function::function_buffer&) [with FunctionObj = std::binder1st<std::mem_fun_t<void, A> >, R = void]':
/opt/local/include/boost/function/function_template.hpp:913:   instantiated from 'void boost::function0<R>::assign_to(Functor) [with Functor = std::binder1st<std::mem_fun_t<void, A> >, R = void]'
/opt/local/include/boost/function/function_template.hpp:722:   instantiated from 'boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = std::binder1st<std::mem_fun_t<void, A> >, R = void]'
/opt/local/include/boost/function/function_template.hpp:1064:   instantiated from 'boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = std::binder1st<std::mem_fun_t<void, A> >, R = void]'
a.cc:7:   instantiated from here
/opt/local/include/boost/function/function_template.hpp:153: error: no match for call to '(std::binder1st<std::mem_fun_t<void, A> >) ()'

最佳答案

因为 bind1st 需要一个二元函数对象。但是,您传递了一个一元函数对象。 C++03 的函数对象绑定(bind)器不像 boost 或 tr1 中的那样复杂。事实上,它们存在一些基本问题,比如无法处理带有引用参数的函数。

既然你已经使用了boost,我推荐使用boost::bind

FunctionCall f = boost::bind(&A::process, this); // yay!

关于c++ - 功能性、bind1st 和 mem_fun,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2917168/

相关文章:

c++在辅助std线程上运行代码

c++ - 如何构建 NS-3 以使用 C++0x/C++11 库?

c++ - 隐式删除子项时继承父项赋值运算符

functional-programming - Elixir 中是否有等效于 Haskell 的 init 函数?

Java 8 将列表按涉及先前元素的条件分组

c++ - 互斥锁操作是否应该因锁定两次而引发 system_error?

c++ - 尝试使用 cmake 编译代码时 undefined reference

c++在解析错误输入时 boost 时间验证

不同类之间非静态函数的C++回调

c# - 性能和代码重用之间的平衡