boost :: bind를 사용하여 결과 함수 객체가 바인드 된 타겟 함수에 대한 인자로 선언되지 않은 객체를 저장할 수 있습니까? 예 :관련없는 객체를 저장하기 위해 boost :: bind를 사용할 수 있습니까?
void Connect(const error_code& errorCode)
{
...
}
// Invokes Connect after 5 seconds.
void DelayedConnect()
{
boost::shared_ptr<boost::asio::deadline_timer> timer =
boost::make_shared<boost::asio::deadline_timer>(ioServiceFromSomewhere);
timer->expires_from_now(
boost::posix_time::seconds(5));
// Here I would like to pass the smart pointer 'timer' to the 'bind function object'
// so that the deadline_timer is kept alive, even if it is not an actual argument
// to 'Connect'. Is this possible with the bind syntax or similar?
timer->async_wait(
boost::bind(&Connect, boost::asio::placeholders::error));
}
ps. 나는 이것을하기위한 기존의 구문에 주로 관심이있다. 나 자신이 직접 코드를 만들 수 있다는 것을 알고있다. 또한 타이머를 수동으로 유지할 수 있다는 것을 알고 있지만이를 피하고 싶습니다.
감사. 그것은 내가 할 계획도 있습니다. 이러한 상황에서 사용할 스마트 포인터 유형 (부스트 및 STL의 범위)에 대한 기본 설정이 있습니까? –
나는 boost :: shared_ptr와 std :: shared_ptr를 사용했다. 객체가 핸들러 객체로 옮겨지면 unqiue_ptr도 작동 할 것입니다. 어쨌든 shared_ptr이 필요했기 때문에 나는 이것에 대한 경험이 없다. – Matthias247
불행하게도 추가 매개 변수를 제안 된대로 사용하면 Visual Studio 2012 컴파일러에서 컴파일 오류가 발생합니다. 오류 C2039 : 'result_type': 'global namespace'의 구성원이 아닙니다. 오류 C2208 : 'boost :: _ bi :: type': 이 유형을 사용하여 정의 된 멤버가 없습니다. C2825 : 'F': '::'이 올 때 클래스 또는 네임 스페이스 여야합니다. –