【发布时间】:2014-02-03 09:40:20
【问题描述】:
我定义了一组函数模板:
template < typename type >
void foo ( type object )
{
// foo the object
}
template < typename type >
void bar ( type object )
{
// bar the object
}
template < typename type >
void baz ( type object )
{
// baz the object
}
现在我想定义一个函数模板,它接受一个指向上述任何函数的指针。
template < /* ??? */ , typename T , typename... TT >
void for_each ( T parameter , TT... other parameters )
{
// process the first parameter using the function pointer defined by the first template parameter
// recursice call to for_each
}
声明第一个模板参数的正确方法是什么?
附注我确实知道有一种解决方法可以将前三个函数分别包装在一个类中并使它们成为静态的。我只是想知道是否有直接的方法可以解决问题。
【问题讨论】:
-
如果你不关心
for_each()里面的type,那么很简单,template<typename F, typename... TT> void for_each(F f, TT... other) -
@Kolyunya 你能在问题中添加一个假设用例吗?
-
@galop1n 用例在问题中明确定义 - 处理异构可变参数包。
-
@Kolyunya 您正在尝试将其编译为 C。
标签: c++ templates c++11 function-pointers c++14