Difference between template function specialization and function overloading.

F

flopbucket

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
 
R

Ron Natalie

flopbucket said:
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.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

flopbucket said:
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 T> inline
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
 
F

flopbucket

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.
 
V

Victor Bazarov

flopbucket said:
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 T> void foo(T const&);

V
 
F

flopbucket

Hi,

It doesn't. It won't compile because

template<> void foo(int);

is NOT a specialisation of the template

template<class T> void 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.
 
V

Victor Bazarov

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

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

is then a specialization?

Precisely!

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top