2010-05-26 3 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

야호를 사용하는 것이 좋습니다. 고맙습니다. –

+0

boost :: bind (& (decltype (* this) :: process), this) 할 수 있습니까? 매크로에 넣으 려합니다. –

+1

@Neil 가장 최근의 초안은'decltype'을 중첩 된 이름 지정자 ('::'앞에있는 것)로 사용할 수있게 만들었습니다. 또한 유형에서 참조를 제거해야합니다. 현재 GCC 버전이 아직 지원하지 않을 수도 있습니다. 이 경우, 대신에 & remove_reference :: type :: process'를 호출하여 작업 해보십시오. –