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/.
experienced cleaning professionals Northbrook ..Microsoft Business Solutions Great Plains as new ERP for multinational... Read More
Microsoft Great Plains, former Great Plains Software Dynamics / eEnterprise... Read More
Microsoft Business Solutions Great Plains fits to majority of horizontal... Read More
Great Plains Fixed Assets Management module is a robust tool... Read More
Microsoft Business Solutions Great Plains, former Great Plains Software Dynamics/eEnterprise... Read More
Your computer cost you from hundreds to thousands of dollars,... Read More
eStore Advantage allows front-office applications to communicate with back-office business... Read More
Microsoft Business Solutions Great Plains serves to the wide spectrum... Read More
Microsoft Great Plains is main accounting / ERP application... Read More
Stop the Runaway MouseWhat's the "runaway mouse?" You've seen it...you... Read More
Are you a whiz at calculating financial information? Not the... Read More
Most computer users use spreadsheets software such as Microsoft Excel... Read More
The stakes are high when considering security, privacy, and savings,... Read More
We'll give you non formal view, based on our consulting... Read More
MS CRM is very close to document workflow automation, including... Read More
For a windows user like me, just can watch with... Read More
Make-or-Break Factors in Success and ProfitabilityFor quick printers, estimating can... Read More
The purpose of one of our projects was MS Exchange... Read More
Document Manager and Version HistoryIn previous articles I have discussed... Read More
This is a short article, written in question/answer/FAQ style to... Read More
When you buy a computer, it most likely comes with... Read More
Have you noticed WordPerfect is gearing up for a comeback... Read More
Fundraising software lets you connect with donors in a way... Read More
In the early days of the personal computer, we're talking... Read More
Spyware and malware are large problems for Internet users today... Read More
best value cleaning service Northbrook ..When you own a small business, time is money. And... Read More
Great Plains Integration Manager scripting and translation - overview for... Read More
Shareware is software that you can try before you buy;... Read More
Considering whether or not your software company should hire a... Read More
ERP Consulting industry is on the way to serve clients... Read More
When you visit department stores and see that majority of... Read More
So, you've bought a new Macintosh, and now you may... Read More
I have yet to see a business that, sometimes in... Read More
Most people don't use Photoshop to its fullest capabilities. Here... Read More
The Internet is reshaping every form of communications medium, and... Read More
Microsoft Great Plains, former Great Plains Software Dynamics / eEnterprise... Read More
Microsoft bought Navision, Denmark based software development company, along with... Read More
Microsoft Business Solutions Great Plains is very good fit for... Read More
Microsoft Business Solutions Great Plains has substantial market share and... Read More
The adware and spyware definitions list is very long. But... Read More
I have always had a tendency to focus on the... Read More
Innovative Maintenance Systems (IMS) is one company that offers solutions... Read More
Think of this, first we had the HAM Radio, then... Read More
Do you remember that frustrating feeling when you find an... Read More
Small can be beautiful! Working with Knoppix for the past... Read More
Features Additionally, Vista will include many other new features.Aero Vista... Read More
Great Plains Software Dynamics, Dynamics C/S+, eEnterprise were written on... Read More
Microsoft Business Solutions Great Plains is very popular ERP platform... Read More
Is Photoshop CS2 worth the upgrade? You bet it is!... Read More
The objective for Zandi Digital is to make available clever... Read More
| Software |