【发布时间】:2016-05-03 09:18:41
【问题描述】:
在这个例子中,我创建了一个以函数为参数的 Functor 类。第二个函子应将第一个函子的对象作为模板参数,并调用第一个函子的函数。我不确定第二个 Functor 的模板应该是什么样子。
这是第一个 Functor,它按预期工作:
typedef float (*pDistanceFu) (float, float);
typedef float (*pDecayFu) (float, float, float);
template <pDistanceFu Dist, pDecayFu Rad, pDecayFu LRate>
class DistFunction {
public:
DistFunction() {}
DistFunction(char *cstr) : name(cstr) {};
char *name;
float distance(float a, float b) { return Dist(a,b); };
float rad_decay(float a, float b, float c) { return Rad(a,b,c); };
float lrate_decay(float a, float b, float c) { return LRate(a,b,c); };
};
在这里我创建了一个专门的仿函数实例:
DistFunction<foo,bar,foobar> fcn_gaussian((char*)"gaussian");
这里我不知道模板的外观如何,将任何类型的 DistFunction<...> 作为参数
template<template<DistFunction> typename = F>
struct functor {
float fCycle;
float fCycles;
functor(float cycle, float cycles) : fCycle(cycle), fCycles(cycles) {}
float operator()(float lrate) {
return (F.lrate_decay)(lrate, fCycle, fCycles);
}
};
我想如何使用第二个仿函数:
typedef DistFunction<foo,bar,foobar> gaussian;
void test() {
functor<gaussian> test(0,1);
}
错误:
error: argument list for class template "DistFunction" is missing
error: expected "class"
error: expected a "," or ">"
【问题讨论】:
-
你将如何使用
functor?functor<what_will_be_specified_here>? -
更新了问题,可能编译器不可能从对象中获取模板信息..,我猜
-
谁提供函子对象?
-
你所有的
char*应该是const char*,然后你不需要转换文字字符串。