467,888 Members | 1,338 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

template function v.s. template class

Hello everyone,
I am feeling template function is more tricky than template class. For
the reason that the compiler will do the matching automatically for
template function, but for template class, developer can assign how to
match.

Sometimes compiler is doing mysterious matching rules for template
function, which makes us confused. Does anyone have the same
senses? :-)

Example,

1. for template function

we define
Expand|Select|Wrap|Line Numbers
  1. template <class Tvoid sort (vector <T>&)
  2.  
when we invoke like,

sort (vector<int>)&, T will automatically matched by compiler to int
-- we have no control. Sometimes, how compiler will do the matching is
mysterious. :-)

2. for template class

developer has full control. For example, when use some template class,
developer can assign the type of parameter,

we define,

Expand|Select|Wrap|Line Numbers
  1. template <class Tclass Foo
  2. {
  3. // ...
  4. }
  5.  
when developer use it, we can explicitly assign the type, like Foo
<intor Foo <bool>.

Any comments or experiences or even disagreement is welcome.
thanks in advance,
George
Dec 13 '07 #1
  • viewed: 1024
Share:
1 Reply
George2 wrote:
Hello everyone,
I am feeling template function is more tricky than template class. For
the reason that the compiler will do the matching automatically for
template function, but for template class, developer can assign how to
match.
First off, the correct names are function and class templates, not the
other way round.

Where the compiler can deduce the type for a function template it makes
sense for it to do so. There is little conceptual difference between
function templates and a set of overloaded functions.

There aren't any instances where the compiler can deduce the type for a
class template, the programmer has to specify it explicitly.

The programmer is free to explicitly state the type for function templates.

--
Ian Collins.
Dec 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Trevor Lango | last post: by
6 posts views Thread by Nobody | last post: by
7 posts views Thread by Lionel B | last post: by
12 posts views Thread by stefan.bruckner | last post: by
8 posts views Thread by Jess | last post: by
2 posts views Thread by Alan | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.