Jump to content

  • Log In with Google      Sign In   
  • Create Account


Improvement in Template Compilation Time


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 jerrinx   Members   -  Reputation: 208

Like
1Likes
Like

Posted 28 May 2012 - 12:49 PM

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

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8049

Like
2Likes
Like

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.

#3 jerrinx   Members   -  Reputation: 208

Like
0Likes
Like

Posted 28 May 2012 - 02:10 PM

Hey Bob,

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

Regards

Edited by jerrinx, 28 May 2012 - 02:21 PM.

JerrinX

#4 _moagstar_   Members   -  Reputation: 465

Like
1Likes
Like

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;


#5 jerrinx   Members   -  Reputation: 208

Like
0Likes
Like

Posted 28 May 2012 - 04:00 PM

You are right !

I was doing that right now when I saw your comment. Posted Image

Thanks for the update guys. Really appreciate it.
JerrinX




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS