Templates
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
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]
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]
You don''t have to specify the template parameters unless they cannot be implied by the function parameters, i.e. the return type is the same as the type used for parameter b. As for the main question try C++ Templates: The Complete Guide
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement