0
\$\begingroup\$

From a game developing point of view, how important do you consider templates to be as a topic to know about in C++? Have you used templates as a part of developing games or similar? Neither do I really understand how important templates can be considered from a general point of view.

\$\endgroup\$

closed as primarily opinion-based by Philipp, Tyyppi_77, Vaillancourt Jan 10 '18 at 14:10

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ Templates are a language-level feature of C++. It is as important to game development as it is to any other application. \$\endgroup\$ – Philipp Jan 10 '18 at 9:34
  • 1
    \$\begingroup\$ @Philipp There is the widespread idea that game programming requires a different subset of programming language features than other programming does, so I think the question is relevant. Note that this comment doesn't endorse that idea. \$\endgroup\$ – Peter Jan 10 '18 at 13:07
4
\$\begingroup\$

There are 2 aspects:

  • Consuming meta-programming, i.e. using templates written by others: The STL (Standard TEMPLATE Library) is a pillar of C++. Nowadays, developing a large C++ application - that includes most games - without using template classes like vector<> or shared_ptr<> means the developer doesn't know how to program in C++.

  • Producing meta-programming, i.e. writing templates: Oftentimes, one can get away without writing templates (often using polymorphism instead) by sacrificing a bit of performance, type safety, or both. If you want cutting edge performance, you need templates, if you don't, you can skip them. Additionally, due to the added type safety provided by templates, their use is strongly recommended if you write a library or other complex components that are to be used by others, as proper use of templates can make your interfaces easy to use right and hard to use wrong. In larger teams, it's common that some people write most of the template code, while most others merely use the templates.

Overall, if you don't want to write templates, C++ may not be the best programming language to chose. If you don't want the highest performance, you could use something like C# instead, unless you're choosing C++ because you're more familiar with the language. Yet if you're not familiar with templates, you're not familiar with C++. There are still plenty of valid reasons to chose C++ regardless, e.g. licensing, corporate constraints, supported target architectures, pre-existing libraries, etc.

\$\endgroup\$
  • \$\begingroup\$ "If you don't want the highest performance, you could use something like C# " - but can't templates also lead to worse performance, such as from compilation-time and size? In that case, isn't that statement incorrect? Btw. thank you so much for the answer (same for all the other nice persons who are willing to help)! \$\endgroup\$ – step Jan 10 '18 at 14:45
  • \$\begingroup\$ @step Compile time doesn't impact the performance of the resulting binaries, if that's what you're worried about. While template heavy code generally takes longer to compile, if you want to optimize compile time you should avoid C++ altogether. \$\endgroup\$ – Peter Jan 10 '18 at 15:14
  • 1
    \$\begingroup\$ @Philipp While they look similar on the surface, they are completely different from the viewpoint of a library writer. The most glaring difference is that generics are not touring complete. Ironically, that means generics are mere code templates, while templates allow generic coding. \$\endgroup\$ – Peter Jan 10 '18 at 17:18
  • 1
    \$\begingroup\$ @Philipp I disagree -- generics (whether in Java or C#) borrow the syntax from templates, but lack most of the features, in particular duck typing. In practice, they are pretty much two completely separate things. \$\endgroup\$ – Quentin Jan 10 '18 at 17:18
  • 1
    \$\begingroup\$ Similar thing for virtual functions/vtables, type info with RTTI, etc. It's one of the things I wish there was more focus on, since otherwise I'd be able to use C++ features a whole lot more often. \$\endgroup\$ – user77245 Jan 10 '18 at 17:44
1
\$\begingroup\$

Templates are a crucial part of C++, they enable flexibility and expressiveness without sacrificing type-safety or efficiency. They are C++'s implementation of generic programming.

They don't have any specific relationship to game development, but like all the other tools in the box they come in handy when you need them. They are typically more useful in "engine"-flavoured tasks, which need their characteristics, and less prominent in the gameplay code which sits on top and doesn't need to be fast or generic (that's also the part that can be written with a scripting language to get other benefits instead).

For an example, here is one of my answers about implementing a kind of component-based dispatching system. You can see that I've used templates extensively (and a hint of macros as well) to create a generic system, and then used it to write terser gameplay code.

\$\endgroup\$
1
\$\begingroup\$

Templates are especially useful for containers or similar types. Since you understand templates generally I will omit an explanation of them, and rather give an example instead.

Templates can be useful in game development (other than for simple containers) when using patterns like the object pool pattern, especially for particle pools. You might want to reuse the implementation for an object pool for different kinds of objects, like enemies, particles, or props. (The linked example does not use templates, but explains the pattern well.)

However, since this is mostly used for optimizing, it is mostly dependant whether one uses an engine that already abstracts instancing functionality, or one even wants to write a custom game engine, in which case it might be inevitable.

\$\endgroup\$
0
\$\begingroup\$

Depends what you mean by 'from a games development point of view'. I've developed games without using templates at all. I've worked for companies which insist on using the standard template library and encourage using templates as well as companies which ban stl & templates completely! If you would like to work in games development then its a good idea to understand them. When abused they can bloat code and make it less readable. Understanding their potential pitfalls is quite important.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.