• Advertisement
Sign in to follow this  

Improvement in Template Compilation Time

This topic is 2064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();
}

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Hey Bob,

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

Regards Edited by jerrinx

Share this post


Link to post
Share on other sites

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;

Share this post


Link to post
Share on other sites
You are right !

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

Thanks for the update guys. Really appreciate it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement