|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Getting Started with Templates |
|
![]() mattd Member since: 2/26/2000 |
||||
|
|
||||
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 Member since: 3/12/2001 From: Australia |
||||
|
|
||||
| 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 Member since: 2/16/2002 From: Amsterdam, Netherlands |
||||
|
|
||||
quote: 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 Member since: 3/12/2001 From: Australia |
||||
|
|
||||
| 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 Member since: 7/25/2003 |
||||
|
|
||||
| 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 Member since: 7/27/2002 From: USA |
||||
|
||||
| > 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] |
||||
|
||||
![]() deepdene Member since: 3/12/2001 From: Australia |
||||
|
|
||||
quote: If I understand the c++ standard you are correct in so far as: template <typename 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<float>. [edited by - deepdene on January 14, 2004 6:39:25 AM] |
||||
|
||||
![]() Ghwerig Member since: 12/9/2003 From: Hawaii, USA |
||||
|
|
||||
quote: 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 Member since: 3/12/2001 From: Australia |
||||
|
|
||||
quote: 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). |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| A great article. Quite clear and straightforward on a subject I've been unclear on for some time. Thanks Glenn! |
||||
|
||||
![]() IronGryphon Member since: 3/27/2005 |
||||
|
|
||||
| Funny, I found the article linked on Programmers Heaven. Good article. Clears up the fog a bit. IronGryphon | Home Page | Blender3D | Wings 3D | The Code Project | A.I. Depot | CGP Beginners Group | Ethereal Designs "A person who won't read has no advantage over one who can't read." ~Mark Twain |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|