Well, if you split them, you won't be able to use the function in other cpps.
As for your original problem, I think template <class T, class T2>
is not the same as
Ok, apparently I didn't try it at work properly, i.e. I had some other error which made me think that "template <class T, class T2>" wouldn't work. Now that I tried again it works. ^^
Thanks for the help! ^^
@hamsterman:
Well, if you split them, you won't be able to use the function in other cpps.
What do you mean by that? I am calling a function I implemented in a cpp file from another cpp and it works.
From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.
?
Because as far as I know, if I put the template function in the cpp like I did above, then the compiler generates an instantiation of the function for each type I declared. Therefore the template is resolved for those specific instantiations at compile time, the instantiations look like normal non-template functions to the compiler again and I can access them freely at runtime.
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.
But that's not true in my case. I'm including only the header in those file which need the template (i.e. no implementation at all is included) and it works.
Well, for one thing I didn't talk about a template function, but a template class containing template functions.
Furthermore you did not create any specific instances of the function like I did in my example. templatevoid A<int>::test(int input); etc.
I don't know if something like that works for template functions, too, though.