Why Not Always Use Templates?
Hello. I've been programming for about two years and have always found a use for C++ templates in every application I make. When I look at source code for games and such, it seems they never use templates. Why is that?
Templates are extremely useful and simple, why not use them on all your classes and functions? I imagine this is because they provide alot of overhead? I don't know.
For such a useful feature, it seems in little use... or am I not looking hard enough?
Thanks.
Until relatively recently, compilers had a severe tendency to make a mess of templates, particularly on platforms that weren't the PC. Before VS 2003, heavy template use was untenable on Windows. I think PS2 had issues for a long time and still might.
Basically, all the source code you see is old. The usaility of templates is a new benefit.
Basically, all the source code you see is old. The usaility of templates is a new benefit.
You should never use something just because you can. There are many, many aspects of software development that do not require the benefits afforded by templates, and thus, templates are not used.
Furthermore, there are times when it isn't practical to implement something using templates, because templates would provide only the illusion of generic code.
Finally, templates are a compile-time beast, and there are situations where that is of no use and a run-time variation must be employed.
Not to mention there's the obnoxious limitations imposed upon template code due to the C++ compilation model (which wouldn't really be fixed with "export," before anybody tries to suggest that).
Furthermore, there are times when it isn't practical to implement something using templates, because templates would provide only the illusion of generic code.
Finally, templates are a compile-time beast, and there are situations where that is of no use and a run-time variation must be employed.
Not to mention there's the obnoxious limitations imposed upon template code due to the C++ compilation model (which wouldn't really be fixed with "export," before anybody tries to suggest that).
Really no more than what you'd get by implementing the functionality manually; modern compilers and linkers are damned good at removing thinks you don't use.
Now that new consoles are coming in, you might see templates used a bit more often... But all the programmers I talked to who work on core aspects of the code hate a few things about them:
1) the potentially bulky syntax of multiple templated things on one line.
2) the overhead it adds to the project if used incorrectly
3) the over-use of templates where other solutions might be more efficient and almost as flexible for that purpose.
Basically templates are not a problem -any more- as long as you don't go overboard on them. The syntax can still get pretty huge if you're using really template heavy code and things -can- run significantly slower if it's used often in the game.
On the tools side it doesn't matter as much.
And yes, as Promit said, most of the code you are looking at is old.
1) the potentially bulky syntax of multiple templated things on one line.
2) the overhead it adds to the project if used incorrectly
3) the over-use of templates where other solutions might be more efficient and almost as flexible for that purpose.
Basically templates are not a problem -any more- as long as you don't go overboard on them. The syntax can still get pretty huge if you're using really template heavy code and things -can- run significantly slower if it's used often in the game.
On the tools side it doesn't matter as much.
And yes, as Promit said, most of the code you are looking at is old.
Quote:Original post by M2tM
...
2) the overhead it adds to the project if used incorrectly
...
The syntax can still get pretty huge if you're using really template heavy code and things -can- run significantly slower if it's used often in the game.
What type of overhead are you refering to (development, runtime, compile-time, etc)? And could you explain to me how a compile-time feature can make a program run significantly slower?
(not meaning to sound like I'm attacking you, these are genuine questions)
Quote:Original post by jpetrie
Really no more than what you'd get by implementing the functionality manually; modern compilers and linkers are damned good at removing thinks you don't use.
I meant using them in situations where other techniques would be more appropriate, especially when using non-type template parameters. Since the OP said "why not use them on all your classes and functions?", I wanted to point out that using them needlessly could lead to bloat. With type variables, I can't see this happening though.
I have worked with a game that had an excessive use of templates, the C++ standard library and boost. The result was a executable of a couple of hundred megabytes that barely fit in the machine's memory. My team had lots of trouble even getting the game to load the main function! Templates are great for making generic containers and functions, but excessive usage can lead to massive code bloat.
I find that very difficult to believe, and would still maintain that something like that is quite the corner case nowadays. Can you provide more information about the specific environment in which you were working?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement