2016-07-31 3 views
3

는 표준 : 기능을 만들려면 결합 :: 여기에 내가 할 것입니다 : -생략의 표준 : : 자리가

std::function<void(int,int,int)> f= 
    std::bind(&B::fb,this, 
     std::placeholders::_1, 
     std::placeholders::_2, 
     std::placeholders::_3 
    ); 

void B::fb(int x,int k,int j){} //example 

B::fb는 세 개의 매개 변수를받을 것이 분명하다.
가독성 & maintainablity을 높이기 위해, 내가 대신 전화를 수 있으면 좋겠다 : -

std::function<void(int,int,int)> f=std::bind(&B::fb,this); //omit _1 _2 _3 

질문
이 자리 표시자를 생략 가능 C의 모든 기능 ++이 있습니까?
자동으로 _1, _2, ...을 호출해야합니다.

나는 "자리 표시 자 생략 C++"을 찾았지만 단서를 찾지 못했습니다.

+4

당신은 자리 표시를 생략 할 수없는 쓰기. 'B :: fb'가 오버로드 될 수 있음을 기억하십시오. – KayEss

+0

그런 다음 "모호한"컴파일 오류가 발생합니다. – javaLover

+0

당신이 페이스 북을 생략하면 글쎄, 지금 오류가 발생합니다. "ambiguius"가 아니라 충분히 가깝습니다. –

답변

7

당신은 기능 도우미 (그 사람이 C++ 14이다)를 만들 수 있습니다

template <class C, typename Ret, typename ... Ts> 
std::function<Ret(Ts...)> bind_this(C* c, Ret (C::*m)(Ts...)) 
{ 
    return [=](auto&&... args) { return (c->*m)(std::forward<decltype(args)>(args)...); }; 
} 

template <class C, typename Ret, typename ... Ts> 
std::function<Ret(Ts...)> bind_this(const C* c, Ret (C::*m)(Ts...) const) 
{ 
    return [=](auto&&... args) { return (c->*m)(std::forward<decltype(args)>(args)...); }; 
} 

후 바로

std::function<void(int, int, int)> f = bind_this(this, &B::fb); 
+0

쿨! 그것은 작동합니다. – javaLover