2016-10-12 2 views
1

함수 포인터의 가변 매개 변수 팩을받는 템플릿 함수를 만들 수 있습니까?함수 인수에 대한 C++ 템플릿 팩

template<ReturnType (*FN)(), ReturnType (*FNX...)()> 
void run() { 
    ... 
    run<FNX...>(); 
    ... 
} 

나는 내가 생각할 수있는 모든 곳에서 ... 배치하려고했습니다,하지만 난 그것을 컴파일 얻을 수 없습니다. 지원되지 않습니까?

답변

2

이 구문을 사용할 수 있습니다,하지만 정말 이상한 같습니다 또한 고급 처리를 수행하는 도우미 클래스를 사용할 수 있습니다

template <typename T> 
using function_ptr = add_pointer_t<enable_if_t<is_function<T>::value,T>>; 

template<function_ptr<void()>... Functions> 
void call_all() 
{ 
    initializer_list<int>{(Functions(), 0)...}; 
} 

:

template<void(*... Functions)()> 
void call_all() 
{ 
    initializer_list<int>{(Functions(), 0)...}; 
} 

내가 비록 유형, 별칭 것 :

using fp = function_ptr<string()>; 

template<fp First, fp... Others> 
struct helper 
{ 
    static void run() 
    { 
     helper<First>::run(); 
     helper<Others...>::run(); 
    } 
}; 

template<fp One> 
struct helper<One> 
{ 
    static void run() 
    { 
     DBG(One()); 
    } 
}; 

template<fp... Functions> 
void run() 
{ 
    helper<Functions...>::run(); 
} 

live demo

+0

위와 같은 방법으로 팩을 실제 템플릿으로 확장하지 않고도 다른 템플릿으로 전달할 수 있습니까? 다음 함수를 호출하기 전에 함수를 하나씩 호출하고 반환 유형을 확인해야합니다. 내 실제 템플릿에는 첫 번째 함수와 나머지 팩이라는 두 개의 인수가 있습니다. – theduke

+0

@theduke가 편집을 확인합니다. – krzaq

+0

굉장합니다, 고마워요. ftw 사용 중 ... – theduke

관련 문제