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/.
scheduled maid service Northbrook ..Our opinion is based on our Microsoft Business Solutions Great... Read More
First we had the original Google search that evolved into... Read More
Since its release in 1987, QuarkXpress had made an immediate... Read More
Looks like Microsoft Great Plains becomes more and more popular,... Read More
Some introduction into Great Plains Software products, now Microsoft Business... Read More
When Windows fails to boot it is normally caused by... Read More
Microsoft Business Solutions Small Business Manager is Great Plains Dexterity... Read More
What is Colossus?Colossus is software licensed to about twenty-five insurance... Read More
Find out why Spyware Removal from computers is important and... Read More
Microsoft Business Solutions Great Plains is mid and even corporate... Read More
How do you run a program on a remote server... Read More
Microsoft Business Solutions Great Plains has substantial market share and... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
When you need a phone number, you do a quick... Read More
I completed an experiment recently. I wanted to find out... Read More
Intro This concise article will tell you in plain English... Read More
Microsoft bought Navision, Denmark based software development company, along with... Read More
New post-recession era has new features, which didn't exist in... Read More
Now is the time to look at an alternative to... Read More
In our small article we'll consider Microsoft Business Solutions Great... Read More
Small can be beautiful! Working with Knoppix for the past... Read More
Around the same time Microsoft made its move with .Net... Read More
In the Clinton era the status quo was simple: you... Read More
Microsoft Business Solutions offers several ERP applications: Great Plains, Navision,... Read More
Having from five to ten and more favorite screensavers is... Read More
reliable maid service Highland Park ..The intentions of this short tutorial are not to teach... Read More
Ok... Where to start?Well, I guess I will start at... Read More
Microsoft Great Plains has substantial mid-market share in the USA... Read More
Using professional icons in your application or website can bring... Read More
The most important things you can do for your computer... Read More
This is the tutorial where we really get into programming.... Read More
Microsoft CRM customization techniques are very diversified and based on... Read More
MSN messenger is a pretty cool invention. I mean I'm... Read More
Executive SummaryAn effective plan for entering, cleaning and updating the... Read More
And kill the best layout software in the process of... Read More
Microsoft Great Plains is becoming more and more popular and... Read More
What is RAID RECOVERY?RAID stands for Redundant Array of Inexpensive... Read More
Now there are Three Steps To Heaven Just listen and... Read More
Whether you are an experienced web programmer or a complete... Read More
Before being able to choose a secure Internet communication system,... Read More
We live in a post-industrial age where information is the... Read More
Well, even if the combination might look very unusual, we... Read More
Lotus Notes Domino is very efficient in electronic document workflow... Read More
When it comes to running an office, the SOHO entrepreneur... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Manufacturing in the USA is far away down from mid... Read More
In the real world a "fire wall" is a fireproof... Read More
Introduction To ISDN, Part III: Configuring PPP PAP AuthenticationNow we... Read More
Microsoft Business Solutions Great Plains is very good fit for... Read More
There is many things more frustrating than surfing a website... Read More
Software |