473,242 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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
1 1200
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
2
by: Alan | last post by:
Does a template class declaration, like template <class T> have to come immediately prior to the declaration of the function, e.g., T do_something (T something) { . . . } that uses it?
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.