Jump to content
  • Advertisement
Sign in to follow this  
OzzoNation

Why Not Always Use Templates?

This topic is 4408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!