Why Not Always Use Templates?

Started by
16 comments, last by JasonBlochowiak 17 years, 6 months ago
Where do you think templates could be leveraged where they're not currently?
[size=2]
Advertisement
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
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
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.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
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.
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
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.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
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.

This topic is closed to new replies.

Advertisement