Skip to main content
gamedev.net
🔒 Locked

Template

Started by Tazel Jan 28, 2003 at 3:45 PM 3 replies 300+ views
Original Post
Tazel
Tazel
What is the definition of a template?
tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
CpMan
CpMan
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.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
Tazel
Tazel
So template is used like a type? And how are the brackets used?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
felisandria
felisandria
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]
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Tazel
Tazel
So a template is something like a function that works for all memory types?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.