templates in C++

Started by
27 comments, last by MARS_999 21 years, 6 months ago
After reading up on templates I have come to the conculsion that they are for lazy programmers. I don''t see the difference if you were to code a function to take explicit parameters vs. general parameters. Just seems lazy and a faster way to whip up some code, and most likely its slower since it has to make up a new function for each type of data that is passed as a parameter. Unless someone can help me see the light I don''t see the need for templates in game programming. Thanks
Advertisement
Why? Because they do exactly the same thing as if you were to make the same function many many times and work at the same speed? They''re more useful with classes than they are with just functions. For instance, every time you want to make a linked list of data you don''t want to have to remake the class and all it''s methods for each different datatype! Templating allows you to write a single templated definition that will create a class for any datatype/data that you pass as a parameter. They''re not for lazy people -- they''re for sensible people.
The different variations of a templated function for a data type is created at compile-time, not at runtime, so the runtime speed would be the same with templates as with handwritten code designed for a specific data type.

The benefits are very obvious with data-storing classes such as linked-lists, arrays, and maps. As Matt has said, instead of writing a different class for each data type, say an array class for an int, a different array class for a float, etc., and debugging all of that, you can just write one template array class that can handle all of those types.

Since a separate version is compiled for each type it''s used with, the slowdown is only at compile time.

Sure, it''s for lazy programmers - but laziness is clearly superior to copying, pasting and modifying some code every time you want to use it with a different type. Why is there anything inherently wrong with laziness?

Also, there''s another use for templates (often useless, but fun)...

  // calculate the xth fibonacci number, where x is a constant#define fib(x) (fib_t<x>::value)template <int x> struct fib_t {  static const int value = fib(x-1) + fib(x-2);};template <> struct fib_t<2> {  static const int value = 1;};template <> struct fib_t<1> {  static const int value = 1;};  
quote:Original post by Beer Hunter
Why is there anything inherently wrong with laziness?
I was wondering the same thing. After all, making things easier and enabling people to be lazier IS what new technology almost always strives for.
One further advantage is code maintenance - there is only one copy of a given function/class in the code, if it is bugged, you only have to correct one instead of going over all copy-pasted (because that''s what you ended up doing, right) slightly tweaked versions.

And that is invaluable.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Fruny
And that is invaluable.
Nah, that''s just "lazyness"

MARS_999: expand on what you mean by ''most likely it''s slower''

what exactly is ''it''? be as detailed as you can.
quote:Original post by MARS_999
After reading up on templates I have come to the conculsion that they are for lazy programmers.

Yep. I''m lazy. I hate having to write far more lines of code than are needed to express a solution. Sorry, what was the question again?
/me thinks Mars_999 didnt understand templates and had to declare it a bad thing.
Domine non secundum peccata nostra facias nobis

This topic is closed to new replies.

Advertisement