I am writing a policy pattern class, which looks like:
template<typename Worker, typename Location, typename Behavior>
class Driver {
public:
Location location;
Behavior behavior;
Worker* worker;
Driver();
void start();
};
in the cpp file, I have this:
template <typename Worker, typename Exchange, typename Behavior>
Driver<Worker, Exchange, Behavior>::Driver(){
worker = new Worker();
};
template <typename Worker, typename Location, typename Behavior>
void Driver<Worker, Location, Behavior>::start(){
;
};
template class Driver<WorkerStruct1, LocationStruct1, BehaviorStruct1>;
here is the problem, I have quite a few of WorkerStruct and LocationStruct and BehaviorStruct. The very reason that I use policy pattern is to avoid the 'combination explosion'.
however, if I have to instantiate all the templates, I will have a big pile of instantiation lines at the bottom of the cpp file. Say, I have 7 for each of these 3 components, then I have 7 * 7 * 7 = 343 lines.
Is this how policy pattern suppose to work?
Some comments asked me why I instantiate the template. I don't know what went wrong, but if I don't instantiate, I get compilation error: "undefined reference to Driver<....>::Driver()"