2013-02-18 3 views
0

멤버 함수의 "유형"을 추론하는 간단한 방법이 있습니까? 내가 수행하는 솔루션을 찾고 있어요(member) 함수의 Deduce 유형

void(int) 

:

struct Sample { 
    void func(int x) { ... } 
}; 

void func(int x) { ... } 

다음과 같은 유형으로 (std::function에 사용되는) : 나는 다음 (회원) 기능 유형을 추론하고 싶습니다 나는 전직을 찾고 있어요

: 예 -

EDIT (! 변수 인수하지 않음) 인수의 변수 수를 지원 decltype 유사 Pression의 -의이 functiontype를 부르 자 - 다음과 같은 의미를 갖는다 :

functiontype(Sample::func) <=> functiontype(::func) <=> void(int) 

functiontype(expr)std::function와 호환되는 유형으로 평가해야합니다.

+2

오버로드 된 함수는 꿈을 죽입니다. – Xeo

+0

@Xeo : 나는 동의하지만 과부하가 걸리지 않은 컨텍스트에서 사용된다고 가정 해 봅시다 ... – MFH

+0

@juanchopanza : 알아요,하지만 "인터페이스"는 [가장 많이 의도 한 것] – MFH

답변

3

이 정보가 도움이 되나요?

#include <type_traits> 
#include <functional> 

using namespace std; 

struct A 
{ 
    void f(double) { } 
}; 

void f(double) { } 

template<typename T> 
struct function_type { }; 

template<typename T, typename R, typename... Args> 
struct function_type<R (T::*)(Args...)> 
{ 
    typedef function<R(Args...)> type; 
}; 

template<typename R, typename... Args> 
struct function_type<R(*)(Args...)> 
{ 
    typedef function<R(Args...)> type; 
}; 

int main() 
{ 
    static_assert(
     is_same< 
      function_type<decltype(&A::f)>::type, 
      function<void(double)> 
      >::value, 
     "Error" 
     ); 

    static_assert(
     is_same< 
      function_type<decltype(&f)>::type, 
      function<void(double)> 
      >::value, 
     "Error" 
     ); 
} 
+0

그 트릭을 해줘서 고마워. – MFH

+0

@MFH : 기꺼이 도와주었습니다. –

+1

그리고 다시, 오버로드 된 함수는 당신의 꿈을 죽입니다. : 3 – Xeo