Development Tip

멤버 함수로 부스트 바인드를 사용하는 방법

yourdevel 2020. 10. 24. 11:57
반응형

멤버 함수로 부스트 바인드를 사용하는 방법


다음 코드로 인해 cl.exe가 충돌합니다 (MS VS2005).
부스트 바인드를 사용하여 myclass 메서드를 호출하는 함수를 만들려고합니다.

#include "stdafx.h"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <functional>

class myclass {
public:
    void fun1()       { printf("fun1()\n");      }
    void fun2(int i)  { printf("fun2(%d)\n", i); }

    void testit() {
        boost::function<void ()>    f1( boost::bind( &myclass::fun1, this ) );
        boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails

        f1();
        f2(111);
    }
};

int main(int argc, char* argv[]) {
    myclass mc;
    mc.testit();
    return 0;
}

내가 도대체 ​​뭘 잘못하고있는 겁니까?


대신 다음을 사용하십시오.

boost::function<void (int)> f2( boost::bind( &myclass::fun2, this, _1 ) );

이렇게하면 자리 표시자를 사용하여 함수 개체에 전달 된 첫 번째 매개 변수가 함수에 전달됩니다. Boost.Bind 에 매개 변수를 처리하는 방법을 알려야 합니다. 표현식을 사용하면 인수를 사용하지 않는 멤버 함수로 해석하려고 시도합니다. 일반적인 사용 패턴은 여기 또는 여기
참조하십시오 .

VC8s cl.exe는 Boost.Bind 오용시 정기적으로 충돌 합니다. 의심 스러운 경우 gcc와 함께 테스트 케이스 를 사용하면 출력을 읽으면 Bind -internals가 인스턴스화 되는 템플릿 매개 변수와 같은 좋은 힌트를 얻을 수 있습니다.

참고 URL : https://stackoverflow.com/questions/2304203/how-to-use-boost-bind-with-a-member-function

반응형