Hey Guys,
The following works for me in Windows.
I have one header file for declaration "TemplateChk.h"
I have one header file for definition "TemplateChkDef.h"
And in your target application you could define concrete classes for the corresponding template classes in Concrete.cpp
So your header file for template classes need not contain implementation, thereby improving compilation time for template
classes, if I am not mistaken.
Just want to know if what I am doing is correct and is it supported on other Platforms like Linux and Apple IOS ?
Thanks
-------------------------------------------------
//TemplateChk.h
-------------------------------------------------
#pragma once
template <class E>
class TemplateChk
{
E val;
public:
void lama();
};
-------------------------------------------------
TemplateChkDef.h
-------------------------------------------------
#pragma once
template <class E>
class TemplateChk
{
E val;
public:
void lama()
{
}
};
-------------------------------------------------
Concrete.cpp
-------------------------------------------------
#include "TemplateChkDef.h"
template TemplateChk<int>;
template TemplateChk<float>;
.....
//Compilation time for this file is reduced since it does not include template class with function definition
-------------------------------------------------
Application.cpp
-------------------------------------------------
#include "TemplateChk.h"
void main()
{
TemplateChk<int> mamma;
mamma.lama();
TemplateChk<float> mamma;
mamma.lama();
}
Improvement in Template Compilation Time
Started by jerrinx, May 28 2012 12:49 PM
4 replies to this topic
Ad:
#2 Moderators - Reputation: 4633
Posted 28 May 2012 - 12:57 PM
It's called explicit template instatiation. However, you should not have the class defined a second time in a second file. In your concrete.cpp, just define the functions as normal outside-the-class member function definitions and instatiate the templates. Your structure with two files; one with the class declaration and one with the definition, is at the very least a maintenance nightmare since you must keep the two files in sync. The language already have what you need so you don't have to hack that part.
#4 Members - Reputation: 461
Posted 28 May 2012 - 03:46 PM
In your concrete.cpp, just define the functions as normal outside-the-class member function definitions and instatiate the templates
If you are doing this as part of a static library I would say it's best to still put these function definitions in a seperate header file (or .ipp or .inl whatever takes your fancy) rather in the instantiating cpp (although as Brother Bob points out the extra class definition in your example code is redundant). This file can then be distributed with your normal headers. This way if you don't provide an explcit instantiation for a particular type, client code can still include the appropriate file and instantiate for that type, otherwise client code can only ever use the types you export.
// SomeStruct.hpp
template <class T>
struct SomeStruct {
void func();
};
// SomeStruct.ipp
template <class T>
void SomeStruct<T>::func() {
// code
}
// instantiation.cpp
#include "SomeStruct.hpp"
template class SomeStruct<int>;
template class SomeStruct<double>;
// client.cpp
#include "SomeStruct.hpp"
#include "SomeStruct.ipp" // explicit instantation for double not provided in SomeLib.lib
SomeStruct<double> object;






