Do you use class templates in C++game programming much?

Started by
16 comments, last by Gorbstein 13 years ago
I am just curious - does anybody here frequently use C++ class templates in game programming? The other day I was looking at some of my code and noticed that I hadn't been using them much and couldn't think of any good reasons or examples to use them. Any input appreciated.
Advertisement
One example we use them for is resource loading. When you have a ton of different types of resources (meshes, images, sounds, etc) that you are loading it might be a good idea to use templates. Obviously it isn't required and the same thing can be accomplished without it.
That depends on what you mean by "use". If you mean writing template classes, I do so only infrequently (outside of answering questions here). If you mean using instances of existing template types I do so all the time (std::vector, std::string, smart pointers, etc.).

That depends on what you mean by "use". If you mean writing template classes, I do so only infrequently (outside of answering questions here). If you mean using instances of existing template types I do so all the time (std::vector, std::string, smart pointers, etc.).


Ya - I use templated types constantly as well. But yes, I was referring to writing templated classes. I'm glad to hear it's pretty rare, because a couple colleagues at my office keep preaching to me how I should be using them more. But, most of the time, I just don't see the point. They're very, very brilliant structure with a lot of generic flexibility. But for game programming, it seems it's just best to use what is quick, fast, and easy. And that never seems to be templates. Interested in hearing people that have a different opinion, though.

a couple colleagues at my office keep preaching to me how I should be using them more.
Is there some context to that?
Just saying "you should use templates more", doesn't make much sense... it's about as meaningful as "you should have more functions", or "there's not enough variables in this file", etc...

I would hope that these 'preachers' would be saying something closer to "this group functions could be made more readable if the logic for this part was pulled out into a templated function".
We use templates when appropriate. Probably slightly more often than many other shops, but certainly not constantly.

The question really is pretty vacuous; what is appropriate for one game or team might vary wildly from what works for someone else.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Actually I use templates generally when creating some generic containers. Be it red-black tree, list, or a bit different implementation of std::vector. I have templates for my simple memory manager and several other things.

There are rather few template classes compared to the rest of my project, but I think they are quite important anyway.
Quite a few functions in my geometry library are templated as their parameters are iterators. They're designed to operate on 2D coordinate sets, but the underlying data is generally 3D. I have an iterator class (also templated) that can step over 3D vertex data and convert to 2D on dereference.

I have a BSP tree algorithm that employs a user supplied function object that handles interpolation between two vertices. This allows the algorithm to operate on generalised vertex data.

There's also a triangulation algorithm that uses the same function object concept, where the user defines the code for the input and output stages, and the algorithm itself is only responsible for producing triangle lists.


The question really is pretty vacuous; what is appropriate for one game or team might vary wildly from what works for someone else.


ouch.

Anyway, the stem of all of this is that some colleagues of mine are preaching the glories of generic algorithms and generic programming. They're arguing that our code should be as generic and portable as possible. I see their reasoning, because it makes perfect sense. But my thought is that if you're targeting one platform and you're making a game after all, making things generic for the sake of being generic might be too much work for the reward. That said, I'm no Donald Knuth. Perhaps I have yet to see the light.
I'd side with you, actually.


Generic code is a waste of time 90% of the time. As my friend and colleague Neil Kirby wisely says, don't try to make it generic/reusable until you've already used it twice. So little code actually does get used generically/reused at all, that it isn't justifiable to invest in excessively fancy tricks up front.

Now, this will again vary in applicability between teams, projects, and even sections of a project. Your math library damn well better be highly reusable code. Your gameplay logic... not so much.


Facts are nice and interesting, but ultimately irrelevant. Context is everything.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement