473,804 Members | 3,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5017
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<typena me Tinline
std::wstring toString( const T & t ) {
std::wstringstr eam 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
6869
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 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
2
5781
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
8
6743
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) template<class X> void myfunc(X par1) { } (in derived class) template<class X>
5
6590
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
2493
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: class A {}; template <typename T> struct Is_C { static const bool value = false;
16
16295
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
3357
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
1950
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
6601
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 trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.