문제

부스트 소스 코드를 검토하는 데 오랜 시간을 소비하지 않고 누군가 부스트 바인드가 구현되는 방법에 대한 간단한 요약을 제공할 수 있습니까?

도움이 되었습니까?

해결책

나는이 부분을 좋아한다 bind 원천:

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

};

실제로 알아야 할 거의 모든 것을 알려줍니다.

그만큼 bind_template 헤더는 인라인 목록으로 확장됩니다. operator() 정의.예를 들어, 가장 간단한 방법은 다음과 같습니다.

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

우리는 볼 수 있습니다 BOOST_BIND_RETURN 매크로는 다음으로 확장됩니다. return 이 시점에서 라인은 더 비슷해졌습니다. return l_(type...).

하나의 매개변수 버전은 다음과 같습니다.

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

꽤 비슷해요.

그만큼 listN 클래스는 매개변수 목록에 대한 래퍼입니다.여기에는 내가 실제로 너무 많이 이해하지 못하는 심오한 마법이 많이 진행되고 있습니다.그들은 또한 과부하가 걸렸습니다. operator() 그게 신비라고 부르는 거야 unwrap 기능.일부 컴파일러 관련 오버로드를 무시하면 많은 작업을 수행하지 않습니다.

// 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();
}

명명 규칙은 다음과 같습니다. F 함수 매개변수의 유형입니다. bind. R 반환 유형입니다. L 매개변수 유형 목록인 경향이 있습니다.다양한 수의 매개변수에 대해 9개 이상의 과부하가 있기 때문에 많은 복잡성도 있습니다.그것에 대해 너무 많이 생각하지 않는 것이 가장 좋습니다.

다른 팁

그런데 만약에 bind_t 다음을 포함하여 축소되고 단순화됩니다. boost/bind/bind_template.hpp , 다음과 같이 이해하기 쉬워집니다.

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

};

바인딩하려는 인수에 대한 멤버 변수를 선언하고 나머지 인수에 대해 오버로드()하는 템플릿 클래스인 것 같습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top