2012-10-25 5 views
8

다음 코드 단편을 참조하십시오. 오버로드 된 기능 foobarstd::bind을 사용하고 싶습니다. 인수가없는 메소드 만 호출합니다.std :: bind 및 오버로드 된 함수

auto a2 = std::bind(static_cast<void(Client::*)(int)>(&Client::foobar), cl, 
        std::placeholders::_1); 
a2(5); 

또한 (값이 아닌 참조에 의해 결합이 떨어져 있음을 알아 cl을)를 람다 캡처 바인딩을 수행 할 수 있습니다 :

#include <functional> 
#include <iostream> 
class Client 
{ 
    public : 
    void foobar(){std::cout << "no argument" << std::endl;} 
    void foobar(int){std::cout << "int argument" << std::endl;} 
    void foobar(double){std::cout << "double argument" << std::endl;} 
}; 

int main() 
{ 
    Client cl; 
    //! This works 
    auto a1 = std::bind(static_cast<void(Client::*)(void)>(&Client::foobar),cl); 
    a1(); 
    //! This does not 
    auto a2= [&](int) 
    { 
     std::bind(static_cast<void(Client::*)(int)>(&Client::foobar),cl); 
    }; 
    a2(5); 
    return 0; 
} 
+0

람다에 '돌아 가기'가 없습니다. – ildjarn

답변

11

당신은 언 바운드 인수 placeholders를 사용할 필요가

auto a2 = [&](int i) { cl.foobar(i); }; 
+0

감사 ecatmur, 그것은 작동합니다. – Atul

+0

그리고 by-value 캡쳐는'[&, i]'또는'[=, & cl]'로 할 수 있습니다. – Xeo