By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

Templates vs overloaded functions?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.