473,503 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates vs overloaded functions?

If a function should work with different types you normally overload it:

void myfun(int a){
// do int stuff
}

void myfun(std::string str){
// do string stuff
}

void myfun(double d){
// do double stuff
}

But you can also use a specialized template function instead:

template<typename T>
void mytemp(T) {
// "default case" if no other match
}

template<>
void mytemp(int a) {
// in case of int
}

template<>
void mytemp(std::string str) {
// in case of string
}
template<>
void mytemp(double d) {
// in case of double
}
The only difference I have found is the correct template is only used if
the arguments match exactly:

http://www.parashift.com/c++-faq-lit...html#faq-35.11

But is that the only difference? Are the types decided at compile time
in both examples etc?
May 11 '07 #1
2 3865
desktop wrote:
If a function should work with different types you normally overload
it:
void myfun(int a){
// do int stuff
}

void myfun(std::string str){
// do string stuff
}

void myfun(double d){
// do double stuff
}

But you can also use a specialized template function instead:

template<typename T>
void mytemp(T) {
// "default case" if no other match
}

template<>
void mytemp(int a) {
// in case of int
}

template<>
void mytemp(std::string str) {
// in case of string
}
template<>
void mytemp(double d) {
// in case of double
}
The only difference I have found is the correct template is only used
if the arguments match exactly:

http://www.parashift.com/c++-faq-lit...html#faq-35.11

But is that the only difference? Are the types decided at compile time
in both examples etc?
No. If your code never makes use of 'mytemp(int)' or 'mytemp(double)',
the code for them will never be generated. I heard a rumour that today
linkers can detect that a [regular] function is not used and throw its
code out, but I would have to see it to believe.

Yes, involvement of conversions is the main reason to prefer regular
[overloaded] functions over template specialisations. If you happen
to use mytemp(blah), and 'blah' is 'short', an overloaded function
'mytemp(int)' will be used, whereas 'mytemp<int>(int)' will never be
picked (and the default case will be then used). Same if 'blah' is
'float' -- the 'mytemp<double>' specialisation isn't applicable.

Also, your example contains 3 overloaded functions and 4 functions
based on the template (one generic and three specialised). They are
not exactly equivalent.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 11 '07 #2
On May 11, 7:39 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
desktop wrote:
But is that the only difference? Are the types decided at compile time
in both examples etc?
No. If your code never makes use of 'mytemp(int)' or 'mytemp(double)',
the code for them will never be generated. I heard a rumour that today
linkers can detect that a [regular] function is not used and throw its
code out, but I would have to see it to believe.
Put them in a separate compilation unit, put the results in a
library, and I don't know of a linker which won't incorporate
just the functions it needs. VC++ has an option to support this
even if the functions aren't in separate compilation units, but
I'm not sure whether it is such a good idea.
Yes, involvement of conversions is the main reason to prefer regular
[overloaded] functions over template specialisations. If you happen
to use mytemp(blah), and 'blah' is 'short', an overloaded function
'mytemp(int)' will be used, whereas 'mytemp<int>(int)' will never be
picked (and the default case will be then used). Same if 'blah' is
'float' -- the 'mytemp<double>' specialisation isn't applicable.
Also, your example contains 3 overloaded functions and 4 functions
based on the template (one generic and three specialised). They are
not exactly equivalent.
That's the big difference, of course. If you have a template,
the compiler will do type deduction on it. If it succeeds, the
results will be added to the overload set. If it fails, they
won't be.

In his first example, the results would have been more or less
the same if he had provided overloaded functions, rather than
template specializations. The template function declaration
would be instantiated, but any time it corresponded to an
overloaded non-template function, that function would be chosen
instead.

There is one big difference, however: you can force the function
you want using templates, by explicitly specifying the template
arguments, e.g.:

mytemp< int >( someDouble ) ;

Of course, explicitly casting the argument to the targeted type
has almost the same results: the only difference is if the
conversion uses an explicit constructor, in which case casting
the argument works, but the above would fail.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6445
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
16
16199
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
44
2376
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
4
1479
by: qning88 | last post by:
I would like to find out how I can effectively make use of templates in theexample below. In Class A, I have 3 overloaded member functions "send" for handling the different messages. Although...
25
3287
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
4
1527
by: Dilip | last post by:
How did the Koenig lookup come to be associated with templates? If I have something like this: (no template code) namespace X { enum E { e1 }; void f(E) { } }
104
4446
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
6
1438
by: pleexed | last post by:
hello, this is my first post in a newsgroup, i hope i do everything right :) first of all, i am sure there have been a lot of "are templates slow?" questions around, but i think what i would...
0
1453
by: WebCM | last post by:
I hope you can spend some time and help me to select proper application design and programming issues. :) I'm making new version of CMS. I've been mostly theorizing about it for a recent months. ...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.