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/.
The Internet is reshaping every form of communications medium, and... Read More
If you are in the market for new staffing software,... Read More
Microsoft has never released a service pack for Windows98 SE,... Read More
It is now common thing when large corporation selects mid-market... Read More
We will base our prognosis on our Microsoft Business Solutions... Read More
What is Software?Software is a set of instruction written to... Read More
You turn on your computer, and it doesn't look quite... Read More
MS CRM is very close to document workflow automation, including... Read More
This is a short article, written in question/answer/FAQ style to... Read More
Once a business idea is selected, it is highly recommended... Read More
In our small article we'll consider Microsoft Business Solutions Great... Read More
Microsoft CRM is new player on the CRM software... Read More
How many steps does it take you to locate and... Read More
Now that spyware is the single most dangerous threat to... Read More
Microsoft Business Solutions Great Plains has I'd say end user... Read More
How to delete the user? This is the first problem... Read More
CD and DVD replication is a process that works by... Read More
Almost all new and major brand of PCs come with... Read More
Microsoft Great Plains and Microsoft Retail Management System (Microsoft RMS)... Read More
IntroductionSurprised, by the title? well, this is a tour of... Read More
In the Clinton era the status quo was simple: you... Read More
Microsoft Business Solutions CRM is present several years on the... Read More
We live in a post-industrial age where information is the... Read More
Microsoft Office program is a programming tool called Visual Basic... Read More
With any good luck and a good amount of hard... Read More
This article is for advanced Microsoft CRM SDK C# developers.... Read More
If you are software developer or database administrator - we... Read More
The US House of Representatives has recently passed the "Spy... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
I provide, here clear explanations and a count of function... Read More
Just when you thought you were Web savvy, one more... Read More
In the early days of the personal computer, we're talking... Read More
Microsoft Great Plains, Navision, Solomon and Axapta are Microsoft Business... Read More
Great Plains Purchase Order Processing (POP) module makes up one-third... Read More
Looks like Microsoft Great Plains becomes more and more popular,... Read More
Microsoft Business Solutions CRM is present several years on the... Read More
Accounts payable is just one area of office management where... Read More
In part III of this ISDN primer, we learned that... Read More
User interfaces and accessibility are some of the most important... Read More
Just imagine: you are walking, say, towards your car, and... Read More
TCO (Total Cost Ownership) is the buzzword in... Read More
Sales are all about leverage, because there is only so... Read More
Finally, you have some time to personalize your desktop with... Read More
Linux essentials:It's free for download but you have to pay... Read More
Having from five to ten and more favorite screensavers is... Read More
Traditionally we were considering functionally rich systems, such as SAP,... Read More
In this article you will find some background information about... Read More
People often ask me: What image file formats will Photoshop... Read More
Great Plains Integration Manager scripting and translation - overview for... Read More
#5 All your hardware components like Printers, PCs etc come... Read More
Stop the Runaway MouseWhat's the "runaway mouse?" You've seen it...you... Read More
Almost all new and major brand of PCs come with... Read More
The Windows Indexing Service provides you with the ability to... Read More
Microsoft Business Solutions Navision is main ERP application for European,... Read More
Microsoft Business Solutions is now in process of creating so... Read More
In a previous article, I wrote about OpenOffice... Read More
When my daughter was getting into AOL instant messaging (AIM)... Read More
Microsoft Business Solutions CRM proved to be reliable solution in... Read More
"Pfishing", sometimes spelled "Phishing", is a word that's used to... Read More
One of the things we can be as certain of... Read More
Microsoft PowerPoint has dramatically changed the way in which academic... Read More
Shareware has been fighting the stigma of being misunderstood for... Read More
With thousands of web pages added to the Net every... Read More
In the real world a "fire wall" is a fireproof... Read More
Microsoft Office program is a programming tool called Visual Basic... Read More
Microsoft bought Navision, Denmark based software development company, along with... Read More
Software |