Why Not Always Use Templates?

Started by
16 comments, last by JasonBlochowiak 17 years, 6 months ago
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.
-----------------------------;(I got something in my eye."You can call me a fat, balding, talentless old queen who can't sing—but you can't tell lies about me."-Elton John"When I was a kid I joined the circus. I did that. It is true. But it's not like you think. There was a guy, he had his own circus. His name was Carol Jacobs and he owned it. It was a small thing."-Christopher Walken"Victory becomes, to some degree, a state of mind. Knowing ourselves superior to the anxieties, troubles, and worries which obsess us, we are superior to them."-Basil King
Advertisement
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.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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).
They can also lead to code bloat if you use them indiscriminately.
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
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.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
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