阅读了关于模板之后,我对它们的编译感到困惑。例如,在一个头,我们定义一个模板 -多个目标文件中的C++模板和汇编
template<typename T>
class Object {
public:
Object();
void hashCode(T& arg){ /* implementation code in header-only. */ }
};
我们使用这个模板在两个源文件 - SourceI.cpp & SourceII.cpp通过包括Object.hpp -
SourceI.cpp
void doSomething()
{
Object<int> intHasher;
intHasher.hashCode();
// Further code...
}
SourceII.cpp
void doNothing()
{
Object<int> notUsedHere;
notUsedHere.hashCode();
}
编译应该产生“int”类型的类实例化代码。 Object <int>类型的代码将存储在哪里。或者将对象<int> :: hashCode()的代码在所有用途中内联?
如果代码没有内联,那么符号冲突会不会链接,因为它们会出现在多个对象文件中?
注意 - 代码是给出一个例子,并没有显示任何目的。
相关:https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – user0042