In this example I create a Functor class taking functions as parameters. The second functor shall take an object of the first functor as template parameter and call a function of the first functor. I am not sure how the template for the second Functor must look.
This is the first Functor, which is working as expected:
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); };
};
Here I create an instance of the functor which is specialized:
DistFunction<foo,bar,foobar> fcn_gaussian((char*)"gaussian");
Here I don't know how the template has to look, to take any type of DistFunction<...> as parameter
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);
}
};
How I want to use the second functor:
typedef DistFunction<foo,bar,foobar> gaussian;
void test() {
functor<gaussian> test(0,1);
}
The errors:
error: argument list for class template "DistFunction" is missing
error: expected "class"
error: expected a "," or ">"
Try
template<typename F>
struct functor {
float fCycle;
float fCycles;
functor(float cycle, float cycles) : fCycle(cycle), fCycles(cycles) {}
float operator()(float lrate) {
return F((char*)"gaussian").lrate_decay(lrate, fCycle, fCycles);
}
};
template<DistFunction> typename = F
This is an unnamed template template parameter with a single non-type parameter of type DistFunction
and default value F
. Since DistFunction
is not a type (it's a class template) and F
does not exist, this makes no sense.
You don't need any template template parameters here. The simple
template<typename F>
struct functor {
should do the job.
If you want to restrict F
, that is, only allow it to accept various instantiations of DistFunction
and nothing else, you need different language facilities, such as static_assert
and/or enable_if
. This is only needed for better error messages in case someone imstantiates functor
incorrectly. Just use F
as if it is a DistFunction
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.