Avoiding Lengthy C++ Template Instantiations
C++ templates are a powerful mechanism that can be used to create generic code. With templates, it is also possible to remove undesirable code duplication, since the same code can then be applied to data of different types.
On the flip-side, however, templates can also create problems due to the potential they have to slow down compilation times. Because all the code in a template is generally available to the compiler when processing translation units, it is difficult to provide separate compilation for templates. An example of library that is victim of this behavior is boost, where typically all the functionality is included in header files. These header files are then included each time the library is referenced in an implementation file, resulting in long build times.
Despite these shortcomings, in some situations it is possible to reduce the amount of work done by the compiler in behalf of templates. This article shows a simple technique that can be used to achieve faster template compilation speeds in the particular case in which desired instantiations are known ahead of time.
Pre-Instantiating Templates
Certain templates are known to be used in only a reduced number of cases. For example, consider a numeric library that creates code for different floating point types. Each class in the library can be instantiated with a particular floating point type, such as double, long double, or float. Consider for instance the following definition:
// file mathop.h
template <class T>
class MathOperations {
public:
static T squared(T value) {
return value * value;
}
// ... };
This class can be used in the following way:
#include <mathop.h>
MathOperations<double> mathOps; double value = 2.5;
cout << "result: " << mathOps.squared(2.5) << endl;
Unfortunately, because the class MathOperations is a template class, we have to include its complete definition as part of the header file, where it can be found by the compiler whenever the class is instantiated.
A possible way to reduce the size of the header file is to pre-instantiate the template for the types that we known in advance.
The first step is to remove the implementation from the header file. This is clearly possible, since you can implement class member functions outside the class declaration (if the class is a template or not). Then, you need to add the implementation to a separate source file. Once this step is done, client code will be able to use the template class interface, but will not be able to generate code. Therefore, for this to work, you need to instantiate the templates on the implementation file.
// file mathop.h
template <class T>
class MathOperations {
public:
static T squared(T value);
// ...
};
// file mathop.cpp
// template member function definition
template <class T>
T MathOperations<T>::squared(T value) {
return value * value;
}
void instantiateMathOps() {
double d = MathOperations<double>::squared(2.0);
float f = MathOperations<float>::squared(2.0);
int i = MathOperations<int>::squared(2);
long l = MathOperations<long>::squared(2);
char c = MathOperations<char>::squared(2);
}
In the example above, I chose to instantiate five versions of the original template for numeric types. The main limitation of this technique, as I mentioned above, is that your clients will not be able to generate templates for the additional types they may want to use. However, in a few situations you may really want to restrict how these templates are used, and the technique above works as desired.
I was wondering if you ever thought of changing the layout of your blog?
Its very well written; I love what youve got to
say. But maybe you could a little more in the way
of content so people could connect with it better. Youve got an awful
lot of text for only having one or 2 images. Maybe you could space it out better?
This post is really a pleasant one it assists new internet people, who are
wishing for blogging.
Hmm is anyone else having problems with the images on this blog loading?
I’m trying to find out if its a problem on my end or if it’s the blog.
Any responses would be greatly appreciated.
Attractive element of content. I simply stumbled upon your web site and in accession capital to say that I
get actually enjoyed account your blog posts. Any way I will be subscribing on your feeds or
even I fulfillment you get admission to consistently fast.
Hello there! This blog post couldn’t be written any
better! Looking through this post reminds me of my previous roommate!
He continually kept talking about this. I am going to
send this information to him. Pretty sure he’ll have a good read.
Thanks for sharing!
Excellent web site you have got here.. It’s hard to find high quality writing like
yours these days. I honestly appreciate people like you!
Take care!!
Wow! This blog looks exactly like my old one! It’s on a
totally different subject but it has pretty much the same layout and
design. Outstanding choice of colors!
No matter if some one searches for his required thing, thus he/she wants to be available that in detail, thus that thing is maintained over here.
Can you tell us more about this? I’d want to find out more
details.
Hurrah, that’s what I was looking for, what a material! present here at this
blog, thanks admin of this website.
I read this article completely concerning the resemblance of most up-to-date and preceding technologies, it’s amazing article.
This is a topic that is close to my heart… Thank you!
Exactly where are your contact details though?
Excellent pieces. Keep writing such kind of information on your blog.
Im really impressed by it.
Hi there, You have done a great job. I will definitely digg it and individually recommend
to my friends. I am sure they’ll be benefited from this site.
Hi! This post could not be written any better!
Reading this post reminds me of my previous room
mate! He always kept talking about this. I will forward this article to him.
Pretty sure he will have a good read. Thank you for sharing!
best stacks for mass
References:
md.swk-web.com
casino island to go
References:
cup-bottle.com
blackjack statistics
References:
welcomeinn.de