Skip to main content
gamedev.net
🔒 Locked

Getting Started with Templates

Started by Myopic Rhino 10 replies 1.5k views
mattd
mattd
Great article, but (you had to see this coming )...

template <typename T, std::size_t size>
class Vector
{
T[size] values;
};


Shouldn''t that be T values[size]; ?

____________
ride the sainted rhythms on the midnight train to romford
deepdene
deepdene
Thanks for that Mattd,

Just emailed the guy in charge of the articles here and hopefully he''ll be able to change the text for ya
Pipo DeClown
Pipo DeClown
quote:
template
class Vector
{
T values[size];
};



Would this work? Can you not only make arrays with the size of a variable with new? (at least this is what I remember.)
int x = 3;
int array[x]; <-- Error


Will the compiler allow this?

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..
deepdene
deepdene
Yes it's perfectly legal.

Reason being that remember that templates are a compile time mechanism, that size is being replaced at compile time.

This is also valid

const int x = 3;
int array[x];

Since the const signifies to the compiler that the variable is static at compile time etc.

BTW one thing.. using int to specify a array size is generally not a good idea.. i.e. one problem is a int allows negative values and negative values don't make much sense in a array. std::size_t is a typedef variable that is used by many functions/operators like sizeof() and std::vector::size and it's conceptually all the available memory addresses. On most implementations std::size_t is a unsigned int.

[edited by - deepdene on January 13, 2004 11:41:24 AM]
psykr
psykr
Maybe some mention of template metaprogramming would be good; I realize that this is an introductory article, but if I had known that template metaprogramming existed back when I had learned templates, I bet I could have come up with some pretty interesting code to mess around with.
duke
duke
> A template parameter has two parts, the type parameter and the identifier. The type parameter defines the identifier to be a typename if declared using the 'class' or 'typename' keywords or a template name if declared using the 'template' keyword. Using the 'typename' or 'class' keywords allows you to pass in any type whether it is a class, struct, enumeration, bool, integral, or floating-point type.

Minor correction: A float (nor is a double) allowed as a template type. I think VC++ allows it, but it is dissallowed by the standard.

It is a good introductory article. I think the way you address what you actually have to do code wise to get started in templates is spot on.





[edited by - duke on January 13, 2004 8:47:15 PM]
super genius
deepdene
deepdene
quote:
Original post by duke
> A template parameter has two parts, the type parameter and the identifier. The type parameter defines the identifier to be a typename if declared using the 'class' or 'typename' keywords or a template name if declared using the 'template' keyword. Using the 'typename' or 'class' keywords allows you to pass in any type whether it is a class, struct, enumeration, bool, integral, or floating-point type.

Minor correction: A float (nor is a double) allowed as a template type. I think VC++ allows it, but it is dissallowed by the standard.

It is a good introductory article. I think the way you address what you actually have to do code wise to get started in templates is spot on.
[edited by - duke on January 13, 2004 8:47:15 PM]


If I understand the c++ standard you are correct in so far as:
template &lttypename T, double D>
is not allowed. It really serves no purpose.

I'm referring to here to the scenario of that vector template (the first one), then later filling it in with Vector3&ltfloat&gt.


[edited by - deepdene on January 14, 2004 6:39:25 AM]
Ghwerig
Ghwerig
quote:
template <typename T>
T foo(T& param)
{
return param + 1;
};

void sample()
{
int delta;
std::cout << foo<int>(delta);
};



template <typename T>
T foo(T& param)
{
return param + 1;
};

void sample()
{
int delta;
std::cout << foo(delta);
};

No need to supply a type argument. The compiler knows what type delta is and choses the right type for you.


[edited by - Ghwerig on January 14, 2004 1:30:55 PM]
deepdene
deepdene
quote:
Original post by Ghwerig
quote:
template <typename T>
T foo(T& param)
{
return param + 1;
};

void sample()
{
int delta;
std::cout << foo<int>(delta);
};



template <typename T>
T foo(T& param)
{
return param + 1;
};

void sample()
{
int delta;
std::cout << foo(delta);
};

No need to supply a type argument. The compiler knows what type delta is and choses the right type for you.


[edited by - Ghwerig on January 14, 2004 1:30:55 PM]


Yep this is true, except there has to be no ambiguity. So if you have type float and type double there is partical ambiguity. Also when it comes to template evaluation c++ doesn''t think far ahead and you can have a whole heap of issues. Not having a parameter type can make for some nice generic programming though (and I probably should of mentioned it in the article).

IronGryphon
IronGryphon
Funny, I found the article linked on Programmers Heaven. Good article. Clears up the fog a bit.
SDBradley
CGP
"A person who won't read has no advantage over one who can't read." ~Mark Twain

Topic Locked

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

Sign in to reply to this topic.