What is the definition of a template?
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
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:
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.
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.
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)
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
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 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]
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)
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement