템플릿에 대해 읽은 후 컴파일에 대해 혼란 스럽습니다. 우리는 두 개의 소스 파일이 템플릿을 사용여러 개의 오브젝트 파일에서 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 형의 클래스 인스턴스화의 코드. 오브젝트 <int> 유형에 대해 코드가 저장됩니다. 또는 객체 <:: hashCode()의 코드가 모든 용도로 인라인 될 것입니까?
코드가 인라인되지 않은 경우 심볼 충돌은 여러 오브젝트 파일에 존재하기 때문에 연결되지 않습니까?
참고 -이 코드는 예제를 제공하기위한 것이며 목적을 나타내지 않습니다.
관련 : https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – user0042