473,394 Members | 1,724 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,394 software developers and data experts.

Difference between template function specialization and function overloading.

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
6 4986
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

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
16
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 ]
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
8
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...

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.