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/.
![]() |
|
![]() |
|
![]() |
|
![]() |
In linux, one of great commands for finding out information... Read More
Great Plains Software Dynamics, Dynamics C/S+, eEnterprise were written on... Read More
First we had the original Google search that evolved into... Read More
Need help making sense of algebra? Have algebra lectures in... Read More
Using professional icons in your application or website can bring... Read More
Microsoft Business Solutions Great Plains, Navision, Axapta, Solomon and CRM... Read More
You've finally created databases that you can actually use to... Read More
Before September 1995, Microsoft Windows was an MS-DOS program. DOS... Read More
Microsoft Word is one of the most popular office applications... Read More
Microsoft Great Plains is main Microsoft Business Solutions accounting package... Read More
Need software to record your voice, streaming audio or musical... Read More
Microsoft Business Solutions Great Plains, Solomon, Navision, Axapta, Microsoft CRM... Read More
Microsoft Business Solutions Great Plains is written in Great Plains... Read More
Former Great Plains Software Dynamics/eEnterprise and currently Microsoft Business Solutions... Read More
Mapping Software Improves Data VisualizationFrom the outset, it is important... Read More
Looks like Microsoft Great Plains becomes more and more popular,... Read More
Bill of Lading is required report for Logistics and Freight... Read More
Most people understand that the "hardware" part of their computer... Read More
If you have Microsoft Great Plains and support it for... Read More
Microsoft Business Solutions Great Plains version 8.5, 8.0, 7.5, Great... Read More
Well, even if the combination might look very unusual, we... Read More
When you own a small business, time is money. And... Read More
The research in the field of Natural Language Processing usually... Read More
How to delete the user? This is the first problem... Read More
Microsoft Business Solutions ? Great Plains has captured the US... Read More
Having from five to ten and more favorite screensavers is... Read More
As of now - Great Plains Dynamics/eEnterprise is transformed/renamed into... Read More
Some introduction into Great Plains Software products, now Microsoft Business... Read More
Microsoft Great Plains - Microsoft Business Solutions accounting and ERP... Read More
Here is some free software tools to help you build... Read More
Currently Microsoft Business Solutions is on the way of creating... Read More
There is many things more frustrating than surfing a website... Read More
Customer Relationship Management (CRM) is a strategy and processes used... Read More
When my daughter was getting into AOL instant messaging (AIM)... Read More
Icons are used everywhere; right from software applications, to internet... Read More
For those who are unclear on the differences between the... Read More
When you own a small business, time is money. And... Read More
Heard about the Quark "killer"?Adobe InDesign CS2. Will it really... Read More
OEComplete is a utility for managing the personal information of... Read More
Microsoft Outlook is one of the most widely used software... Read More
This article is for advanced Microsoft CRM SDK C# developers.... Read More
Microsoft Business Solutions products: Great Plains, MS CRM, Navision, Axapta,... Read More
The destruction of the Soviet Union about 15 years ago,... Read More
During the years of our consulting practice, which comes back... Read More
Microsoft CRM is new player on the CRM software... Read More
ERP Consulting industry is on the way to serve clients... Read More
Whether you are an experienced web programmer or a complete... Read More
The Software 2005 conference is now a wrap. This conference,... Read More
Microsoft Business Solutions Great Plains, Navision, Axapta, Solomon and CRM... Read More
Rapid Application Development (RAD) is a software development methodology. In... Read More
An operating system (abbreviated OS) is essentially the path through... Read More
Microsoft Business Solutions CRM proved to be reliable solution in... Read More
Sometimes your PC will start acting strange for no apparent... Read More
Once upon a time not so long ago, there was... Read More
Microsoft Great Plains is becoming more and more popular and... Read More
Now that spyware is the single most dangerous threat to... Read More
Linux essentials:It's free for download but you have to pay... Read More
Did you ever want to erase everything on your computer?... Read More
It's all about turn times in the eMedia industry! The... Read More
If you are software developer or database administrator - we... Read More
Microsoft CRM is CRM answer from Microsoft and attempt to... Read More
In part III of this ISDN primer, we learned that... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Adobe Photoshop is one of the world's leading graphics editing... Read More
Lotus Domino/Notes ? Microsoft Great Plains tandem as ERP with... Read More
Anyone who has ever used Microsoft Word knows that it... Read More
Software |