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>&)
[/Code]

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
{
// ...
}
[/Code]

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

Re: template function v.s. template class by Ulrich

Ulrich
Wed Dec 12 23:49:26 PST 2007

George wrote:
> 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>&)
> [/Code]
>
> when we invoke like,
>
> sort (vector<int>)&, T will automatically matched by compiler to int

That doesn't even compile. Anyway: for a function, the compiler can deduce
the template parameters from the arguments (sometimes at least), while for
a class it can't. That's the reason that it is done for functions. However,
you can also explicitly specify the template parameters, e.g. if the
compiler is guessing wrong:

sort<int>(my_vector);


BTW: you could argue that the compiler could also guess the template
parameters for template classes sometimes. This is indeed true, but it is
simply not supported by the C++ standard, which is why compilers just don't
do it.


Uli


Re: template function v.s. template class by George

George
Thu Dec 13 00:40:00 PST 2007

Thanks Uli,


I want to confirm that you mean we can either choose implicit approach (let
compiler deduce) or explicit approach (let developer decide)?


regards,
George

"Ulrich Eckhardt" wrote:

> George wrote:
> > 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>&)
> > [/Code]
> >
> > when we invoke like,
> >
> > sort (vector<int>)&, T will automatically matched by compiler to int
>
> That doesn't even compile. Anyway: for a function, the compiler can deduce
> the template parameters from the arguments (sometimes at least), while for
> a class it can't. That's the reason that it is done for functions. However,
> you can also explicitly specify the template parameters, e.g. if the
> compiler is guessing wrong:
>
> sort<int>(my_vector);
>
>
> BTW: you could argue that the compiler could also guess the template
> parameters for template classes sometimes. This is indeed true, but it is
> simply not supported by the C++ standard, which is why compilers just don't
> do it.
>
>
> Uli
>
>

Re: template function v.s. template class by Ulrich

Ulrich
Thu Dec 13 00:51:03 PST 2007

George wrote:
> I want to confirm that you mean we can either choose implicit approach
> (let compiler deduce) or explicit approach (let developer decide)?

Which part of what I wrote leaves that in question?


BTW: http://www.netmeister.org/news/learn2quote.html. This is not about
bothering you, but your questions indicate that your thoughts are
unstructured and using these techniques helps against asking questions that
were answered again. FWIW, I will from now on start ignoring your questions
if they indicate that you simply didn't bother reading the given answers.

Uli

[discarded useless fullquote]



Re: template function v.s. template class by George

George
Thu Dec 13 01:35:02 PST 2007

Hi Uli,


I am sorry for any inconvenience. :-)

I have read your post and my post is just my further thinking about this
question. Maybe I do not choose the proper word.

Sorry again and I hope you can continue to discuss with me in the future. I
learned a lot from you.


regards,
George

"Ulrich Eckhardt" wrote:

> George wrote:
> > I want to confirm that you mean we can either choose implicit approach
> > (let compiler deduce) or explicit approach (let developer decide)?
>
> Which part of what I wrote leaves that in question?
>
>
> BTW: http://www.netmeister.org/news/learn2quote.html. This is not about
> bothering you, but your questions indicate that your thoughts are
> unstructured and using these techniques helps against asking questions that
> were answered again. FWIW, I will from now on start ignoring your questions
> if they indicate that you simply didn't bother reading the given answers.
>
> Uli
>
> [discarded useless fullquote]
>
>
>

Re: template function v.s. template class by Ulrich

Ulrich
Thu Dec 13 03:18:05 PST 2007

George,
I asked you one simple question but your reply to that doesn't in the least
approach that question. Also, against the given advise, you annoy the world
by dumping irrelevant content on us. What kind of image do you think that
conveys? My current image is that your problems don't deserve my time.

Uli

Intentional fullquote following...

George wrote:
> Hi Uli,
>
>
> I am sorry for any inconvenience. :-)
>
> I have read your post and my post is just my further thinking about this
> question. Maybe I do not choose the proper word.
>
> Sorry again and I hope you can continue to discuss with me in the future.
> I learned a lot from you.
>
>
> regards,
> George
>
> "Ulrich Eckhardt" wrote:
>
>> George wrote:
>> > I want to confirm that you mean we can either choose implicit approach
>> > (let compiler deduce) or explicit approach (let developer decide)?
>>
>> Which part of what I wrote leaves that in question?
>>
>>
>> BTW: http://www.netmeister.org/news/learn2quote.html. This is not about
>> bothering you, but your questions indicate that your thoughts are
>> unstructured and using these techniques helps against asking questions
>> that were answered again. FWIW, I will from now on start ignoring your
>> questions if they indicate that you simply didn't bother reading the
>> given answers.
>>
>> Uli
>>
>> [discarded useless fullquote]
>>
>>
>>



Re: template function v.s. template class by Tim

Tim
Fri Dec 14 23:23:12 PST 2007

George <George@discussions.microsoft.com> wrote:
>
>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? :-)

Uli has already responded to your specific questions, but I'm wondering why
this seems to you to be a problem.

>Example,
>
>1. for template function
>
>we define
>[Code]
>template <class T> void sort (vector <T>&)
>[/Code]
>
>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.
>:-)

Why is it mysterious? This is exactly what I want it to do -- it's the
reason I would write it as a template function.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.