0
boost :: bind를 사용하여 함수 객체를 만들려고해도 HEAP에 만들어진 객체를 바인딩하고 싶습니다. 지연 호출을 위해. 예제 코드는 아래와 같습니다.boost :: bind를 사용하여 자동 릴리즈 "heap"리소스를 바인딩하는 함수 객체 만들기
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/typeof/typeof.hpp>
#include <iostream>
using namespace boost;
class CTest : public noncopyable
{
public:
CTest():mInt(0){ std::cout << "constructor" << std::endl; }
~CTest(){ std::cout << "destructor" << std::endl; }
int mInt;
};
int getM(CTest * t)
{
return t->mInt;
}
function<int()> makeF()
{
// create some resource on HEAP, not on STACK.
// cause the STACK resource will be release after
// function return.
BOOST_AUTO(a , make_shared<CTest>());
// I want to use bind to create a function call
// wrap the original function and the resource I create
// for delay call.
//
// I use shared_ptr to auto release the resource when
// the function object is gone.
//
// Compile ERROR!!!
// cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'CTest *'
//
return bind<int>(getM , a);
}
int main(int argc, char* argv[])
{
BOOST_AUTO(delayFunc , makeF());
delayFunc();
return 0;
}
위의 코드는 예제 코드입니다. 그러나 나는 그것이 내가 원하는 것을 보여주고 현재의 오류라고 생각합니다. 현재 내가 가지고있는 경우,
return bind<int>(CGetM() , a);
그러나 :
class CGetM
{
public:
typedef int result_type;
int operator() (shared_ptr<CTest> t)
{
return getM(t.get());
}
};
그리고이 같은 코드를 대체 :
현재, 나는 단지 아래와 같이 원래의 기능을 래핑하는 함수 객체를 사용할 수 있다고 생각 올바른 인수를 적용하기 위해 getM과 같은 많은 원래 함수를 함수 객체에 래핑하는 것은 실제로 큰 작업입니다. 어떤 종류의 팁이나 다른 유용한 유틸리티 클래스가 있다면 부스트에서 더 지능적으로 우아하게 처리 할 수 있을지 모르겠습니다.
의견을 보내 주시면 감사하겠습니다. 감사.
나는이 테스트를하고,이 위대하다! 바인딩이 그런 식으로 사용할 수 있는지 모르겠다. foo(), bar(), foo (bar())가 될 것 같다. – winterTTr