Help understand Templates

R

RoLo

Can someone explain or give me a link on how to create templates?
specially on how to use the template<> thingy. For example I have
seen this:

template <class T>
template<typename T>
template <>

each on almost identical situations... so whats the difference?

for example:

#include <iostream>

template <class T>
class Test
{
public:
T i;
};

template <typename T>
class Test2
{
public:
T i;
};

using std::cout;

int
main(void)
{
Test<int> i;
Test2<int> j;

i.i=1;
j.i=1;

cout << i.i << '\t' << j.i << '\n';
return 0;
}

whats the difference between Test and Test2 templates??
 
R

red floyd

RoLo said:
Can someone explain or give me a link on how to create templates?
specially on how to use the template<> thingy. For example I have
seen this:

template <class T>
template<typename T>
template <>
[redacted]

The first two are semantically identical. The third is the way to
introduce a specialization.

When I write a template, I use the typename/class equivalence to
document the code. If a template is intended to be used with any type
as a template parameter, then I use "typename". If I intend it to be
used with a class parameter, then I use "class". Note that the language
doesn't enforce such restrictions, it's just a convention to document
the intended usage.
 
R

RoLo

The first two are semantically identical. The third is the way to
introduce a specialization.

When I write a template, I use the typename/class equivalence to
document the code. If a template is intended to be used with any type
as a template parameter, then I use "typename". If I intend it to be
used with a class parameter, then I use "class". Note that the language
doesn't enforce such restrictions, it's just a convention to document
the intended usage.

Thanks for your help :-D
 
G

Greg Comeau

Can someone explain or give me a link on how to create templates?
specially on how to use the template<> thingy. For example I have
seen this:

template <class T>
template<typename T>

See http://www.comeaucomputing.com/techtalk/template/#typename
template <>

The <> allows us to establish a specialization. This is used
when we know something about a special instance that can
be handled better than the generalized description.

But we really can't/shouldn't do this justice on a NG, so I would
suggest getting a good C++ text (see http://wwww.comeaucomputing.com/booklist
 
R

RoLo

wow, awesome reply Greg :-D
Thanks!

You have cleared my doubts. I will surely buy a C++ Templates book
anyway, I really need one as reference.

Tanks again everyone for your replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top