Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does template instantiation bloat matter in practice?

Tags:

c++

templates

d

It seems that in C++ and D, languages which are statically compiled and in which template metaprogramming is a popular technique, there is a decent amount of concern about template instantiation bloat. It seems to me like mostly a theoretical concern, except on very resource-constrained embedded systems. Outside of the embedded space, I have yet to hear of an example of someone being able to demonstrate that it was a problem in practice.

Can anyone provide an example outside of severely resource-limited embedded systems of where template instantiation bloat mattered in practice and had measurable, practically significant negative effects?

like image 232
dsimcha Avatar asked Nov 20 '09 16:11

dsimcha


2 Answers

There's little problem in C++, because the amount of template stuff you can do in C++ is limited by their complexity.

In D however ... before CTFE (compile-time function evaluation) existed, we had to use templates for string processing. This is also the reason big mangled symbols are compressed in DMD - the strings used as template arguments become part of the mangled symbol names, and when instantiating templates with longer segments of code (for instance) the resulting symbol sizes would spectacularly explode the object format.

It's better nowadays. But on the whole, templates still cause a lot of bloat for a simple reason - they parse faster and are more powerful than in C++, so people naturally use them a lot more (even in cases that technically wouldn't need templates). I must admit I'm one of the prime offenders here (take a look at tools.base if you like, but be sure to keep a barf bag handy - the file is effectively 90% template code).

like image 122
FeepingCreature Avatar answered Nov 15 '22 20:11

FeepingCreature


Template bloat is NOT an issue (It is a mental problem not a code problem).

Yes it can get big. But what's the alternative?
You could write all the code yourself manually (once for each type). Do you think writting it manually will make it smaller. The compiler only instanciate the versions it actually needs and the linker will remove multiple copies spread over compilation units.

So there is no actual bloat.
It is just building what you use. If you use a lot of different types you need to write more code.

like image 25
Martin York Avatar answered Nov 15 '22 20:11

Martin York