Is it possible to use a template on a constructor, for example:
class Class {
public:
template<typename T>
Class(T arg);
};
And the cpp file:
#include "class.h"
template<typename T>
Class::Class (T arg) {
// do something
}
The constructor is called with:
int a = 0;
Class c {a};
When compiling this code, I get the following error:
/usr/bin/ld: /tmp/ccNzp4z7.o: in function `main':
main.cpp:(.text+0x1c): undefined reference to `Class::Class<int>(int)'
collect2: error: ld returned 1 exit status
The use for this would be a constructor like:
Class(std::unordered_set<T> choices, std::unordered_map<Direction, std::function<std::string(T)>> mapping);
Where each object in choices
would be iterated over for each Direction
in the map and passed to the corresponding function, in order to generate the object. In this case, the type used has no bearing on the class after the constructor finished executing, so I don't think that it makes sense to use a template on the entire class.