I have some template code that I'd rather have saved in a CPP file rather than inline in the header.
I know this is possible if you know which template types will be utilised.
As an example:
.h file
class foo
{
public:
template <typename T>
void do(const T& t);
};
.cpp file
template <typename T>
void foo::do(const T& t)
{
// Do something with t
}
template void foo::do<int>(const int&);
template void foo::do<std::string>(const std::string&);
The final two lines are important since the foo::do template function is only used with ints and std::strings, therefore those definitions indicate that the programme will link.
My concern is whether this is a bad hack or if it will work with other compilers/linkers.
I am currently just using this code with VS2008, but I want to transfer it to more settings in the future.