After reading about templates, I am confused about their compilation. For example, in a header we define a template as -
template<typename T>
class Object {
public:
Object();
void hashCode(T& arg){ /* implementation code in header-only. */ }
};
We use this template in two source files - SourceI.cpp & SourceII.cpp by including Object.hpp -
SourceI.cpp
void doSomething()
{
Object<int> intHasher;
intHasher.hashCode();
// Further code...
}
SourceII.cpp
void doNothing()
{
Object<int> notUsedHere;
notUsedHere.hashCode();
}
The compile should generate code for the class instantiation for the "int" type. Where will be the code stored for Object<int> type. Or will the code for Object<int>::hashCode() be inlined in all uses?
If the code is not inlined, then won't symbols clash will linking because they would be present in multiple object files?
NOTE - The code is for giving a example and doesn't show any purpose.