Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Whoknewb

Templates

This topic is 5583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I believe I understand how to usetemplates but I''m looking for a more indepth understanding. Bare with me I have lots of questions For example why when you have a function you can''t do this // Decleration template < class T > T sqaure(T a); // Definition template < class T > T square(T b) { return b*b; } That won''t compile because in the decleration I used the variable a and the decleration I used b. Also why do I have to put template before each function I use it in? Why can''t I just declare T to be of type template? And finally what is so significant about the keyword class? Whats the purpose of the word class in template

Share this post


Link to post
Share on other sites
Advertisement
ok, so i'm not really a template wiz... but as far as i know,

instead of declaring the function with a variable name, you can leave it open, such as

// Declaration.
template < class t >
t foo< t > // no variable name, just type

as for putting template before each function, why not? makes the code less obscure, imho. but i'm not sure it's necessary?
i usually do like
template < typename t >
class foo
{
foo(t);
~foo();

bar(t);
};

class can be replaced with typename, it's just a generic for type (...lack of better term)...
'template < typename t >' can be used instead of 'template < class t >'

[edited by - prankster on February 7, 2003 8:47:09 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Whoknewb
For example why when you have a function you can''t do this

// Decleration
template < class T >
T sqaure(T a);


// Definition
template < class T >
T square(T b)
{
return b*b;
}

That won''t compile because in the decleration I used the variable a and the decleration I used b.


The error here is that the function declared is called ''sqaure'' while the function you define is called ''square''. The name is merely a placeholder (and is optional); I know of no reason why the code should not work, save for the spelling error.

Share this post


Link to post
Share on other sites
quote:
Original post by Whoknewb
Also why do I have to put template before each function I use it in? Why can''t I just declare T to be of type template?


The template<> code is what declares T to be a templated type - this is why you must include it anywhere you want T to be a ''parametric'' type.

Since you can overload functions, you need to perfectly match the signature of a forward declaration.

//consider something like this
template<typename T>
T foo();

//if you don''t tell it T is a template parameter, the
// compiler wants to find a type called T
typedef T int;
T foo();


quote:

And finally what is so significant about the keyword class? Whats the purpose of the word class in template


You need to tell it whether the template parameter is a type or an integral. You can also say template<int I>. Originally they re-used the keyword class, and during standardization they decided to add the keyword ''typename'' since the re-use of class was a bit of a stretch and rather confusing. So class and typename mean the same thing inside of a template declaration.

Share this post


Link to post
Share on other sites
quote:
The error here is that the function declared is called ''sqaure'' while the function you define is called ''square''.


By accident I spelt it wrong in the post but it doesn''t work even if you were to spell it correctly.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!