2014-04-01 2 views
3

는 다음 두 선언을 고려 (인수의 유형에 관계없이)? 당신에게 도움이 될 수는 Sfinae는 0 또는 1 매개 변수 중 하나와 기능에

+1

첫째, 나는 그런 식으로, 인수 기본 템플릿을 사용하지 않는 것이 좋습니다. 그리고 일반적으로 유형에 관계없이 하나의 인수로 함수를 호출 할 수 있는지 여부를 알 수 없습니다. 함수로 SFINAE를 사용하는 경우에는 무엇이든 변환하는 더미 유형이 작동 할 수 있지만 경우에 따라 작동하지 않을 수 있습니다. – Jamboree

+1

하나의 인수로 함수를 호출 할 수 있지만 인수의 형식을 알 수없는 경우 왜주의해야합니까? 인수의 형식을 모르는 경우 호출 할 수 없습니다. 호출 할 수있는 유일한 시나리오는 인수 유형을 알고있는 시나리오입니다. –

답변

0

다음 특성 :

#include <type_traits> 
#include <functional> 

template <typename T> 
struct arity : public arity<decltype(&T::operator())> {}; 

template <typename C, typename Ret, typename... Args> 
struct arity<Ret(C::*)(Args...) const> : 
    std::integral_constant<std::size_t, sizeof...(Args)> 
{ 
}; 

// Do the same for other (11) combination of volatile, const, reference method. 

// function pointer 
template<class R, class... Args> 
struct arity<R(*)(Args...)> : public arity<R(Args...)> 
{}; 

template<class R, class... Args> 
struct arity<R(Args...)> : std::integral_constant<std::size_t, sizeof...(Args)> 
{ 
}; 

그리고

template <class Function, 
      class = typename std::enable_if<arity<typename std::remove_reference<Function>::type>::value == 0>::type> 
void apply(Function&& function); 
+0

함수 개체는 어떻게됩니까? – jrok

+0

functor 클래스 ('operator()')와 std :: function'의 특성을 완성 할 수 있습니다. – Jarod42

+1

FWIW, [has_call] (https://github.com/jamboree/boost.has_call) 특성이 있습니다 (참고 : Boost에 포함되지 않음) – Jamboree

관련 문제