• Advertisement
Sign in to follow this  

Why Not Always Use Templates?

This topic is 4171 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
Quote:
Original post by Ezbez
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)


It isn't as simple as: "Templates cause bloat" it's the entire object oriented and abstracted container approach to creating low-level items where simple functions and C style programming might suffice. Certainly there is a time and a place for that approach and it is -very- nice from a conceptual perspective and can speed development time, but by the very nature of creating components which are more complex than they need to be you are adding code bloat and I believe that is the real itch of the matter.

Now, I'm all for templates and OO programming, but people who apply it blindly to every situation regardless of the purpose often end up with less efficient and much larger programs. Not only that, but templates do indeed bloat code when they are used in situations which do not actually require them.

http://www.accu.org/acornsig/public/cathlib/docs/bloat.html

Here is a very short article describing some of the problems with templates themselves... I just pulled this off the web and haven't looked into it in detail, I'm just passing down what I've heard from more advanced programmers than myself. I understand it on a higher level, but can't point out the assembly differences. I imagine with newer systems that this is much less of an issue, but as late as the PS2 it was something to consider.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
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?


Lets just say that it was a recently published game that my team was porting to another platform.

Share this post


Link to post
Share on other sites
Quote:
Original post by M2tM
Now, I'm all for templates and OO programming, but people who apply it blindly to every situation regardless of the purpose often end up with less efficient and much larger programs.


I believe you meant them. Templates are used for generic programming, and only incidentally used in conjunction with object-oriented programming.

OP: Templates in C++ are bulky and interact badly with the non-existent module system. They have two ideal use cases: unrelated type parametrization (where other solutions don't work) and compile-time metaprogramming. In other cases, they require unnecessary work.

Note that this is generally not the case for type parametrization in other languages, such as C#, Java or ML-family languages, where generics or type generalization is both very light memory-wise and very fast. Unlike templates, these do not support metaprogramming.

Share this post


Link to post
Share on other sites
My mistake, yes, I meant the plural and yes I know what a template is and I realize that it is not directly tied into OOP... The complaint I've heard has been applied to bulky constructs that make excessive use of templates which is why I mention it in that context.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Complexity versus flexibility is perhaps one of the most fundamental tradeoffs in computer programming. Thinking is required here.

The C++ FAQ Lite has a section on templates, which I just realized I haven't read over yet. x.x


That's exactly it. Using complex and overly flexible methods or creating complex infastructures to solve simple problems is the problem that was related to me, and what I was trying to summarize, however you have done so much more elegantly. I'd rate you up if I hadn't in the past.

Share this post


Link to post
Share on other sites
I've used templates in shipping titles for PS2 and other consoles. In some cases, it was entirely the right call. In other cases, it lead to unnecessary bloat and/or obfuscation.

As with most other things, treating templates as a golden hammer is a bad idea.

Share this post


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

  • Advertisement