Executor class has template of type P and it takes a P object in constructor. Algo class has a template E and also has a static variable of type E. Processor class has template T and a collection of Ts.
Question how can I define Executor< Processor<Algo> >
and Algo<Executor>
? Is this possible? I see no way to defining this, its kind of an "infinite recursive template argument"
See code.
template <class T>
class Processor {
map<string,T> ts;
void Process(string str, int i)
{
ts[str].Do(i);
}
}
template <class P>
class Executor {
P &p;
Executor(P &inp) : p(inp) {}
void Bar(string str, int i) {
p.Process(str,i);
}
Execute(string str)
{
}
}
template <class E>
class Algo
{
static E e;
void Do(int i) {}
void Foo()
{
e.Execute("xxx");
}
}
main ()
{
typedef Processor<Algo> PALGO; // invalid
typedef Executor<PALGO> EPALGO;
typedef Algo<EPALGO> AEPALGO;
Executor<PALGO> executor(PALGO());
AEPALGO::E = executor;
}
EDIT ****************************
A little clarification. Executor is a singleton that provides a service. All Algo objects need the services of Executor. Executor will sometimes generate reports that need to be sent to a specific Algo object. They get sent to the correct Algo through Processor.
Basic issue is that Algo is needed to define Executor and Executor is needed to define Algo.
AFAICS, you cannot do that with the same
Executor
type. Otherwise you would have to defineIt might work, if you define it with some other type, provided that makes any sense technically
or with
typedef
You can use inheritance.
Else, this is not possible.
Since Executor is a singleton, you can move its definition out of Algo either in it's own singleton class or in Executor. Then make all the Algo function that need to know about Executor template member functions.
Solved! see comments. //****
Tried reproducing your code, not quite sure what you are trying to achieve. For starters, this is what I modified it to:
Modified Proc to Processor in Executor definition - (what is Proc?) and gave it template argument, in the typedef Processor> PALGO; Then AEPAGO::E --> thats a template param, not class Algo member - so AEPAGO::e. Now you will get an error that can be more manageable. It needs a copy constructor to convert types.