C++ Function templates are those functions which can handle different data types without separate code for each of them. For a similar operation on several kinds of data types, a programmer need not write different versions by overloading a function. It is enough if he writes a C++ template based function. This will take care of all the data types.
There are two types of templates in C++, viz., function templates and class templates. This article deals with only the function templates.
There are lot of occasions, where we might need to write the same functions for different data types. A favorite example can be addition of two variables. The variable can be integer, float or double. The requirement will be to return the corresponding return type based on the input type. If we start writing one function for each of the data type, then we will end up with 4 to 5 different functions, which can be a night mare for maintenance.
C++ templates come to our rescue in such situations. When we use C++ function templates, only one function signature needs to be created. The C++ compiler will automatically generate the required functions for handling the individual data types. This is how a programmer's life is made a lot easier.
C++ Template functions - Details:
Let us assume a small example for Add function. If the requirement is to use this Add function for both integer and float, then two functions are to be created for each of the data type (overloading).
int Add(int a,int b) { return a+b;} // function Without C++ template
float Add(float a, float b) { return a+b;} // function Without C++ template
If there are some more data types to be handled, more functions should be added.
But if we use a c++ function template, the whole process is reduced to a single c++ function template. The following will be the code fragment for Add function.
template
T Add(T a, T b) //C++ function template sample
{
return a+b;
}
This c++ function template definition will be enough. Now when the integer version of the function, the compiler generates an Add function compatible for integer data type and if float is called it generates float type and so on.
Here T is the typename. This is dynamically determined by the compiler according to the parameter passed. The keyword class means, the parameter can be of any type. It can even be a class.
C++ Template functions - Applicability:
C++ function templates can be used wherever the same functionality has to be performed with a number of data types. Though very useful, lots of care should be taken to test the C++ template functions during development. A well written c++ template will go a long way in saving time for programmers.
About The Author
Muthukumar
More articles can be found at http://www.codersource.net/.
shuttle to Midway Beardstown .. Lockport Chicago limo O’HareHow would you like to prevent spyware and adware from... Read More
IBM Lotus Notes Domino and Microsoft CRM (Client Relation Management)... Read More
Configuring PPP PAP AuthenticationNow we know how the ISDN link... Read More
Follow the steps below to quickly design, generate, and deploy... Read More
With thousands of web pages added to the Net every... Read More
What is Interactive Mapping?Interactive mapping is a visual display medium... Read More
Does Microsoft care for WebmastersIt's always been a problem with... Read More
Although statistics often is blamed for various deadly sins --... Read More
Words we choose to describe things and phenomena often show... Read More
You would like to protect your documents, wouldn't you? Reasons... Read More
Homeland security, airport security, Internet security ??" these days we???re... Read More
What is Snort?Snort is an open source network intrusion detection... Read More
Language development computer: Computer-based method for aiding language development seems... Read More
If you copy something from a Web site or elsewhere...... Read More
Microsoft CRM and IBM Lotus Notes Domino seem to be... Read More
Preventive Maintenance (PM) is defined as scheduled work done on... Read More
OEComplete is a utility for managing the personal information of... Read More
Microsoft Great Plains and Microsoft Retail Management System (Microsoft RMS)... Read More
So, why should you use any O/R mapping tool? I... Read More
It is now common thing when large corporation selects mid-market... Read More
Midsize business or non-profit organization should decide if one-vendor solution... Read More
Usually, the easiest way to tell you have spyware is... Read More
TikiWiki is open source software - it is written in... Read More
There are so many different programs that clutter up your... Read More
Whether you need to close a sale, gather end-user feedback,... Read More
Green Bay Hummer H2 SUV rentals ..One of the main reasons business owners and entrepreneurs use... Read More
IBM Lotus Notes Domino and Microsoft CRM (Client Relation Management)... Read More
Microsoft Great Plains is now targeting large and midsize businesses... Read More
Is your PC is slow and wimpy? Then you need... Read More
Microsoft Great Plains as ERP and Microsoft CRM as... Read More
SAP Inc., a global leader in client/server enterprise application software... Read More
Which Type of Shop Can Rely On A Home Built... Read More
Some companies that are in need of fleet management may... Read More
If you have been running Windows XP for a couple... Read More
We don't think about mainframe software pricing anymore, we just... Read More
Anti-virus software is used to find, remove or fix files... Read More
Microsoft Business Solutions Great Plains and MS CRM (client relation... Read More
GroupwareThe internet is full of 1.5 million to 7 million... Read More
This article is the third of a series of articles... Read More
Almost all new and major brand of PCs come with... Read More
Microsoft Business Solutions Great Plains was designed back in the... Read More
Microsoft bought Navision, Denmark based software development company, along with... Read More
Blue Cross and Blue Shield of Hawaii (HMSA) found itself... Read More
It is possible that if one avoided all sources of... Read More
I suggest that you do not spend a lot of... Read More
Just the thought of a duel-boot scares many people away,... Read More
Scrapbooks are very popular these days. I think that almost... Read More
Microsoft Great Plains is one of the Microsoft Business Solutions... Read More
Does Microsoft care for WebmastersIt's always been a problem with... Read More
Find out why Spyware Removal from computers is important and... Read More
Software |