1
是否可以创建一个模板函数来接收函数指针的可变参数包?函数参数的C++模板包
template<ReturnType (*FN)(), ReturnType (*FNX...)()>
void run() {
...
run<FNX...>();
...
}
我试着放在我能想到的所有地方...
,但我不能得到它来编译。这不支持?
是否可以创建一个模板函数来接收函数指针的可变参数包?函数参数的C++模板包
template<ReturnType (*FN)(), ReturnType (*FNX...)()>
void run() {
...
run<FNX...>();
...
}
我试着放在我能想到的所有地方...
,但我不能得到它来编译。这不支持?
你可以使用这个语法,但它看起来很奇怪:
template<void(*... Functions)()>
void call_all()
{
initializer_list<int>{(Functions(), 0)...};
}
我别名类型,虽然:
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)...};
}
您还可以使用辅助类做更多先进的加工:
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();
}
太棒了,但是我怎样才能将这个包传递给另一个模板(如我的例子)而不将它扩展到实际的调用?我需要逐个调用函数并在调用下一个函数之前检查它们的返回类型。我的实际模板有两个参数,第一个功能和其余部分作为一个包。 – theduke
@theduke检查编辑 – krzaq
真棒,谢谢一堆。使用ftw ... – theduke