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

Difference between template function specialization and function overloading.

P: n/a
Could someone explain to me what the difference is between function
template specialization and function overloading?

I guess overloading can change the number of parameters, but otherwise
they seem very similar to me - i.e. they both provide specialized
functions for depending on the parameter types.

Thanks

Aug 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
flopbucket wrote:
Could someone explain to me what the difference is between function
template specialization and function overloading?

I guess overloading can change the number of parameters, but otherwise
they seem very similar to me - i.e. they both provide specialized
functions for depending on the parameter types.

Thanks
Well, overloading has nothing to do with templates. Overloading
means defining more than one function signature for the same name.
There's no need for any constraint on the arguments:

void foo(int);
void foo();
void foo(std::string, double);

are all different and valid overloads. Other than the overload
resolution, they are all unique entities...they can have different
access classes, can be virtual or not on each function, etc..

A template provides a generic implementation for a variety of
types, but they all (save for defaulted args) the same number
of arguments. A specialization is just that, a user provided
implementation of the template instantiation rather than allowing
the compiler to generate one from the generic template.

Templated functions can not be virtual and they all have the
same access.
Aug 16 '06 #2

P: n/a

flopbucket wrote:
Could someone explain to me what the difference is between function
template specialization and function overloading?

I guess overloading can change the number of parameters, but otherwise
they seem very similar to me - i.e. they both provide specialized
functions for depending on the parameter types.
They do different things in different ways. Overloading allows you to
call a different function depending on the arguments given:

void foo( int );
void foo( double );
void foo( int, int );

Are all fine, but you can't also have:

int foo( int );

Template specialisation allows you to define a function that works for
any type and specialising it allows you to specify a different
implementation for some of them. Here is a good example:

template<typename Tinline
std::wstring toString( const T & t ) {
std::wstringstream ss;
ss << t;
return ss.str();
}

This allows you to turn most types into a string automatically, but is
silly for strings so we do this:

template<inline
std::wstring toString< std::wstring >( const std::wstring &t ) {
return t;
}

What we cannot do though is this:

template<inline
std::wstring toString< double >( double v, int places ) {
//...
}

We can also have a difference in return type so we can do this:

template< typename T >
T variant_cast( const VARIANT &v );

And then specialise this for those types that need to pull data out of
the variant in odd ways.

Hope this helps.
K

Aug 16 '06 #3

P: n/a
>
They do different things in different ways. Overloading allows you to
call a different function depending on the arguments given:

void foo( int );
void foo( double );
void foo( int, int );

Are all fine, but you can't also have:

int foo( int );
Right, this I know and understand. But what about the following:

template<class T>
void foo(const T& t) { ... }

if I have (or can I have?) also:

void foo(int t) { ...}

is that then an unrelated function ? I believe it is not a
specialization because it does not have template<in front of it. In
that case, what if I also have:

template<>
void foo(int t) { ... }

How does that interact with plain void foo(int) ?

Thanks for all the assistance.

Aug 17 '06 #4

P: n/a
flopbucket wrote:
>They do different things in different ways. Overloading allows you to
call a different function depending on the arguments given:

void foo( int );
void foo( double );
void foo( int, int );

Are all fine, but you can't also have:

int foo( int );

Right, this I know and understand. But what about the following:

template<class T>
void foo(const T& t) { ... }

if I have (or can I have?) also:

void foo(int t) { ...}

is that then an unrelated function ? I believe it is not a
specialization because it does not have template<in front of it. In
that case, what if I also have:

template<>
void foo(int t) { ... }

How does that interact with plain void foo(int) ?
It doesn't. It won't compile because

template<void foo(int);

is NOT a specialisation of the template

template<class Tvoid foo(T const&);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '06 #5

P: n/a
Hi,

>
It doesn't. It won't compile because

template<void foo(int);

is NOT a specialisation of the template

template<class Tvoid foo(T const&);
Ok, but the reason for that is the paremeter type? i.e.

template<void foo(const int&) { ... }

is then a specialization?

Thanks for the information.

Aug 17 '06 #6

P: n/a
flopbucket wrote:
>It doesn't. It won't compile because

template<void foo(int);

is NOT a specialisation of the template

template<class Tvoid foo(T const&);

Ok, but the reason for that is the paremeter type? i.e.

template<void foo(const int&) { ... }

is then a specialization?
Precisely!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.