2008-09-22 11 views

답변

24

은 내가 bind 소스의이 작품을 좋아한다.

bind_template 헤더는 인라인 operator() 정의의 목록으로 확장됩니다. 예를 들어, 단순한 : 라인이 더 return l_(type...)처럼 그래서

result_type operator()() 
{ 
    list0 a; 
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0); 
} 

우리는이 시점에서 returnBOOST_BIND_RETURN 매크로가 확장을 볼 수 있습니다.

한 매개 변수 버전

은 여기에 있습니다 :

template<class A1> result_type operator()(A1 & a1) 
{ 
    list1<A1 &> a(a1); 
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0); 
} 

그것은 꽤 비슷하다.

listN 클래스는 매개 변수 목록의 래퍼입니다. 여기에 많은 깊은 마술이 일어나서 나는 너무 많이 이해하지 못합니다. 또한 신비한 unwrap 함수를 호출하는 operator()을 오버로드했습니다. 일부 컴파일러 특정 과부하 무시, 그것은 많이하지 않습니다

// unwrap 

template<class F> inline F & unwrap(F * f, long) 
{ 
    return *f; 
} 

template<class F> inline F & unwrap(reference_wrapper<F> * f, int) 
{ 
    return f->get(); 
} 

template<class F> inline F & unwrap(reference_wrapper<F> const * f, int) 
{ 
    return f->get(); 
} 

의 이름 지정 규칙은 것 같다 : Fbind에 함수 매개 변수의 유형입니다. R이 반환 유형입니다. L은 매개 변수 유형의 목록이되는 경향이 있습니다. 매개 변수의 수에 따라 9 개 이상의 과부하가 없기 때문에 많은 복잡성이 있습니다. 너무 많이 머물지 않는 것이 좋습니다. bind_tboost/bind/bind_template.hpp 등으로 축소하고 단순화하면 그런데

+2

이 가 왜'#입니다 ... 나에게 간단하지 않는 것 BOOST_BIND_RETURN return'을 정의해야합니까? 왜 돌아 오지 않는가? – Ha11owed

+0

나는 아직도 그것을 얻지 않는다. 'bind_t'의 생성자를 호출하는 것은 무엇입니까? – ThomasMcLeod

+2

@ Ha11owed 그들은 반환 값이없는 템플릿에 헤더를 사용할 수 있기 때문에! –

0

필자는 바인드하려는 인수에 대한 멤버 변수를 선언하고 나머지 인수에 대해 overloads()를 선언하는 템플릿 클래스라고 생각합니다.

template<class R, class F, class L> class bind_t 
{ 
public: 

    typedef bind_t this_type; 

    bind_t(F f, L const & l): f_(f), l_(l) {} 

#define BOOST_BIND_RETURN return 
#include <boost/bind/bind_template.hpp> 
#undef BOOST_BIND_RETURN 

}; 

거의 당신이 알아야 할 모든, 정말 당신을 알려줍니다 :

2

는이 같은 이해하기 쉽게된다 다음

template<class R, class F, class L> 
class bind_t 
{ 
    public: 

     typedef bind_t this_type; 

     bind_t(F f, L const & l): f_(f), l_(l) {} 

     typedef typename result_traits<R, F>::type result_type; 
     ... 
     template<class A1> 
      result_type operator()(A1 & a1) 
      { 
       list1<A1 &> a(a1); 
       return l_(type<result_type>(), f_, a, 0); 
      } 
    private: 
     F f_; 
     L l_; 

};