Jump to content
  • Advertisement

Archived

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

MARS_999

templates in C++

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!