template function v.s. template class

G

George2

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
Code:
template <class T> void sort (vector <T>&)

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,

Code:
template <class T> class Foo
{
    // ...
}

when developer use it, we can explicitly assign the type, like Foo
<int> or Foo <bool>.

Any comments or experiences or even disagreement is welcome.


thanks in advance,
George
 
I

Ian Collins

George2 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top