#### Archived

This topic is now archived and is closed to further replies.

# templates in C++

This topic is 5600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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;};

##### Share on other sites
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.

##### Share on other sites
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 ]

##### Share on other sites
quote:
Original post by Fruny
And that is invaluable.
Nah, that''s just "lazyness"

##### Share on other sites
MARS_999: expand on what you mean by ''most likely it''s slower''

what exactly is ''it''? be as detailed as you can.

##### Share on other sites
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?

##### Share on other sites
/me thinks Mars_999 didnt understand templates and had to declare it a bad thing.

##### Share on other sites
quote:
Original post by Beer Hunter
Also, there's another use for templates (often useless, but fun)...

Naw, we actually use that stuff.... but... but.... macros?!?!? And the more general with user-defined start points look more interesting too:

        template<unsigned long n, unsigned long a, unsigned long b>struct fib_seq {  static const unsigned long val = fib_seq<n-1,a,b>::val + fib_seq<n-2,a,b>::val;};template<unsigned long a, unsigned long b>struct fib_seq<0,a,b> {  static const unsigned long val = a;};template<unsigned long a, unsigned long b>struct fib_seq<1,a,b> {  static const unsigned long val = b;};

But of course, that would mean we are putting the poor users of a compiler without partial template specialisation at a loss.... oh well.

Yours truly,
Henrik Stuart

[edited by - muer on October 21, 2002 7:12:24 AM]

##### Share on other sites
The three chief virtues of a programmer are: Laziness, Impatience and Hubris -- Larry Wall

##### Share on other sites
"Sorry boss, I can''t make the deadline because I don''t like templates, and instead I''m going to code up and debug 4 separate solutions to the same problem, so I need 4 times as long."

That''d go over well.

##### Share on other sites
Rewrite my height-balanced binary tree because I need it to work with yet another data type? I don''t think so. That was the first time I used templates. I just had to make sure that the data types I used supported < > <= >= and ==. Hooray for operator overloading!

##### Share on other sites
What business do I have using a compiler when I could code everything in 1''s and 0''s?

I must be lazy.

Shame on me.

And using pre-written polygon-drawing libraries instead of writing my own software renderer? *BONK* What was I thinking?

Thanks for opening my eyes.

##### Share on other sites
quote:
Original post by Matt Calabrese
They''re more useful with classes than they are with just functions.

Wrong, I use templates with functions just as well as with classes( ever seen Macros replaced by templated functions???????? )... How about making sure that you know what you''re saying before posting...

[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com]

##### Share on other sites
quote:
Original post by Waverider
What business do I have using a compiler when I could code everything in 1''s and 0''s?

I must be lazy.

Shame on me.

And using pre-written polygon-drawing libraries instead of writing my own software renderer? *BONK* What was I thinking?

Thanks for opening my eyes.

and don''t forget _BUYING_ _EXISTING_ hardware! i mean, really! gpu''s! you _BUY_ a gpu. you just plug it into a mainboard! a _MAINBOARD_ you _BUYED_!! and the cpu! and the ram! and the harddrives!!!

and you _INSTALL_ an _EXISTING_ operating system!! (i don''t say you buy:D)..

yes dude, we have to do it for ourselfes...

about templates: get into metaprogramming, read some nice books, learn loki, boost, std/stl, and you know that theres so much more than lacyness.

i never say its not because we _are_ lacy.. its there to save us from making lacynessbugs actually..

"take a look around" - limp bizkit

##### Share on other sites
quote:
Original post by Cyberdrek

Wrong, I use templates with functions just as well as with classes( ever seen Macros replaced by templated functions???????? )... How about making sure that you know what you're saying before posting...

What I meant by that was the fact that when you template a class, anything in your class can use the template parameter and now you have an entire object which is templated not a single function. When you template a class it's as though any member functions where templated as well. Templating classes just takes it to a higher level. I didn't say you'd use them less with functions, just that a single templated class has the potential to be a lot more than one templated function.

Are you off your period yet? I don't know why you were all defensive.

EDIT: this forum messes up nest quoting.

[edited by - Matt Calabrese on October 21, 2002 4:32:42 PM]

##### Share on other sites
and now you''re being offensive

somebody knock these boys heads together...

... in a nice way

peace

##### Share on other sites
quote:
Original post by Zorak
/me thinks Mars_999 didnt understand templates and had to declare it a bad thing.

Next people will start to say that Functions are lazy too!

##### Share on other sites
What the fuck is wrong with these people. Somebody makes an observation, expresses an opinion, POOF!

He''s the devil incarnate.

Perspective, people.

##### Share on other sites
if you look at peoples profiles you can see that some people just say daft stuff and never come back for a follow up post

##### Share on other sites
quote:
Original post by petewood
if you look at peoples profiles you can see that some people just say daft stuff and never come back for a follow up post

Really??? I made the observation since I am learning templates and from what I have read looked like an easier way to code a function to accept many different data types. I will 99% of time go with easier isn''t always better. So thats why I asked why is templates so great. I can see the need for operator overloading I kind of like them. They make sense and are not quite as generic. BTW never said I was a C++ expert.

"You have Freedom, sir"
"Were do I have freedom"
"My FREEDOM isn''t for sale"

##### Share on other sites
quote:
Original post by muer
Naw, we actually use that stuff.... but... but.... macros?!?!
Macros give the illusion of being a function call, while still being able to use compile-time constants. Although I'd normally leave macros out altogether, they do have their use in explaining template metaprogramming to mere mortals.

quote:
Original post by daerid
What the fuck is wrong with these people. Somebody makes an observation, expresses an opinion, POOF!
This insanity again?

If someone reaches a conclusion before asking for the facts, this is the risk they take.

[edited by - Beer Hunter on October 21, 2002 6:34:37 PM]

##### Share on other sites
quote:
Really??? I made the observation since I am learning templates and from what I have read looked like an easier way to code a function to accept many different data types.

What do you feel is the advantage of writing each function separately?

quote:
I will 99% of time go with easier isn''t always better.

Why?

quote:
So thats why I asked why is templates so great.

Because, amongst other things, they mean you don''t have to write the same function or class over and over and over again.

quote:
I can see the need for operator overloading I kind of like them.

Templates are IMO one of the more important uses of ad hoc polymorphism. That is to say, templates transform operator overloading from being simply nice syntactic sugar into something genuinely useful and damn near irreplacable.

quote:
They make sense and are not quite as generic. BTW never said I was a C++ expert.

So much is evident.

You appear to think that their being for lazy programmers is a Bad Thing. Why?

You appear to think that a templated function will be slower than equivalent hand-written overloads. Why?

You appear to think that the easier a solution is, the more likely it is to be inferior. Why?