Home » Community » Forums » » Getting Started with Templates
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Getting Started with Templates
Post Reply 
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

 User Rating: 1611   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1086   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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..

 User Rating: 1469   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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]

 User Rating: 1086   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 1130   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

> 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]

 User Rating: 968   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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 <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]

 User Rating: 1086   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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).



 User Rating: 1086   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

A great article. Quite clear and straightforward on a subject I've been unclear on for some time.

Thanks Glenn!

 User Rating: 1015    Report this Post to a Moderator | Link

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

 User Rating: 1167   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: