2010-05-26 54 views
4

爲什麼不能編譯?功能,bind1st和mem_fun

#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> >)()' 

回答

7

由於bind1st需要一個二進制函數對象。但是,您傳遞一個一元函數對象。 C++ 03的函數對象綁定程序並不像boost或tr1中那樣複雜。事實上,他們遭受基本問題,如不能用參考參數處理函數。

既然你已經使用升壓,我建議確屬自用的boost::bind

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

耶。謝謝。 – 2010-05-26 22:23:56

+0

can not boost boost :: bind(&(decltype(* this):: process),this)? 我想把它變成一個宏。 – 2010-05-26 22:27:08

+1

@Neil只有最近的草稿才能使用'decltype'作爲嵌套的名稱說明符('::'之前的東西,你還需要從類型中刪除引用,tho)。我認爲目前的GCC版本可能還不支持它。在這種情況下,嘗試使用'&remove_reference :: type :: process'來解決它。 – 2010-05-26 22:30:20