我有一些功能模板,例如
template
void foo(T);
template
void bar(T);
// others
我需要将每一个传递给一种算法,它将被称为各种类型的算法.
template
void some_algorithm(F f)
{
// call f with argument of type int
// call f with argument of type SomeClass
// etc.
}
我不能传递我的函数模板uninstantiated,但我不能用任何特定的类型实例化它,因为some_algorithm将需要调用它与几个不同类型的参数.
我可以将我的函数模板修改为多态函数对象,例如
struct foo_polymorphic
{
template
void operator()(T t)
{
foo(t);
}
};
然后将其作为some_algorithm(foo_polymorphic())传递.但是这需要为每个我的功能模板编写一个单独的适配器.
是否有一种通用的方法来使函数模板适应为多态函数对象,即我可以为每个需要适应的函数模板重新使用的一些机制,而不必为每个功能对象单独声明一些内容?