Archived

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

Template

This topic is 5432 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

I would define it something like this:

A construct that passes an arbitrary construct (not specifically type, you can also do things like template< std::size_t value> at compile time.

And by example:


template< class ThisIsAType, std::size_t ThisIsAValue >
class Foo {

};


There are also a couple other traits that are imporant, in that a template instantiation with different parameters represents a distinct type.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
No, not like a type, really.

The major use of templates is containers, mostly because containers need to be able to hold all sorts of things. For instance, would you rather write 8 different linked list classes to hold the 8 different lists your application needs, or would you prefer to just write one templated linked list class that can handle every type you throw at it? After all, every single linked list is going to basically have the same functionality of insertion, deletion, iteration, etc.

That's why we have template container libraries like STL (standard TEMPLATE library) which give us a bunch of container types, with all the functionality that container type could possibly use... BUT, with the bonus that we can put any type we want into it.

That's what the <> are for. That's where you tell the template what type you want it to use in that particular instantiation of the template. So, you might have a list< int > and a list< string > and so on. You can keep your types and not have to go to void*, while not having to write a class for every single type.

If you want to learn more about templates in the STL context, which is a good way to get used to them, go to http://codeguru.earthweb.com/cpp/stlguide/stl_index_cat.shtml

-fel

[edited by - felisandria on January 28, 2003 5:56:05 PM]

Share this post


Link to post
Share on other sites