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/.
cleaning service near Bannockburn ..You have gotten those E_Mails buy software at deep discounts.... Read More
It could just be me, but my experiences with document... Read More
Mapping Software Improves Data VisualizationFrom the outset, it is important... Read More
Should one use Windows Update?This topic has good and valid... Read More
Whether you are a small consultancy firm, a medium sized... Read More
Microsoft PowerPoint has dramatically changed the way in which academic... Read More
Internet worms. Is your PC infected?If your computer has become... Read More
Once upon a time not so long ago, there was... Read More
Are Spreadsheets Robbing your Enterprise of Competitive Advantage?'90% of "average"... Read More
Three highly respected names in Human Resources have joined forces... Read More
I suggest that you do not spend a lot of... Read More
The objective for Zandi Digital is to make available clever... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Whether you have used Microsoft Word for years, have just... Read More
Introduction to C++Why Learn C++?C++ may at first seem like... Read More
Let's say that you have a software project that's under... Read More
Microsoft Business Solutions CRM data conversion deserves FAQ type of... Read More
Microsoft Great Plains may be recommended for international freight forwarding... Read More
Traditionally we were considering functionally rich systems, such as SAP,... Read More
Looking at all the ads which promise to get rid... Read More
Music downloads are off the charts! We're listening to digital... Read More
Looks like Microsoft Great Plains becomes more and more... Read More
Configuring PPP PAP AuthenticationNow we know how the ISDN link... Read More
Microsoft SQL Server is the leader for inexpensive and middle... Read More
If there still are few unprotected computers left, I haven't... Read More
pet-friendly home cleaners Mundelein ..With the advent of 'Service Pack 2' for Windows XP... Read More
And kill the best layout software in the process of... Read More
This tutorial covers OLAP solutions used by Data warehouses and... Read More
Remember back in the days where screensavers were the coolest... Read More
Did you ever want to erase everything on your computer?... Read More
Bar charts, bar graphs, and any other chart or graph... Read More
In this small article we will be looking at the... Read More
The vast majority of us will have, at some point,... Read More
The Internet is reshaping every form of communications medium, and... Read More
In the real world a "fire wall" is a fireproof... Read More
Have you noticed WordPerfect is gearing up for a comeback... Read More
There are plenty of articles out there about how to... Read More
Mike Dunville* had a decision to make. As the new... Read More
Configuring PPP PAP AuthenticationNow we know how the ISDN link... Read More
With many manufacturing shops heading over seas in favor of... Read More
The first thing that you will notice about Linux Red... Read More
Music downloads are off the charts! We're listening to digital... Read More
IntroductionDuring the early years of our modern computer era, very... Read More
Usually, the easiest way to tell you have spyware is... Read More
Are you lost in the mess of documents that get... Read More
If someone entered your home, uninvited and installed numerous cameras... Read More
Now that spyware is the single most dangerous threat to... Read More
Executive SummaryAn effective plan for entering, cleaning and updating the... Read More
In order to meet regulatory and corporate compliance requirements reporting... Read More
Heard about the Quark "killer"?Adobe InDesign CS2. Will it really... Read More
Software |