Skip to main content
GameDev.net gamedev.net
🔒 Locked

Improvement in Template Compilation Time

Started by jerrinx May 28, 2012 at 6:49 PM 3 replies 1.8k views
Original Post
jerrinx
jerrinx
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 TemplateChk
{
E val;

public:
void lama();
};

-------------------------------------------------
TemplateChkDef.h
-------------------------------------------------

#pragma once

template
class TemplateChk
{
E val;

public:
void lama()
{
}
};

-------------------------------------------------
Concrete.cpp
-------------------------------------------------
#include "TemplateChkDef.h"

template TemplateChk;
template TemplateChk;
.....

//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 mamma;
mamma.lama();

TemplateChk mamma;
mamma.lama();
}
JerrinX
Brother Bob
Brother Bob
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.
jerrinx
jerrinx
Hey Bob,

Thanks a lot. Template instantiation is what I wanted !!

Regards
JerrinX
_moagstar_
_moagstar_

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;
jerrinx
jerrinx
You are right !

I was doing that right now when I saw your comment. smile.png

Thanks for the update guys. Really appreciate it.
JerrinX

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.