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/.
Great Plains Software Dynamics, Dynamics C/S+, eEnterprise were written on... Read More
Healthcare facilities such as clinics, hospitals, and biomedical laboratories can... Read More
What is Tripwire?Tripwire is a form intrusion detection system (IDS)... Read More
As of now - Great Plains Dynamics/eEnterprise is transformed/renamed into... Read More
Having from five to ten and more favorite screensavers is... Read More
While Ukraine is becoming a new popular IT outsourcing destination,... Read More
When Windows fails to boot it is normally caused by... Read More
The Windows registry is a huge database that ensures normal... Read More
The objective for Zandi Digital is to make available clever... Read More
Adobe Photoshop is one of the world's leading graphics editing... Read More
What is a Web Database?A web database is a database... Read More
"Pfishing", sometimes spelled "Phishing", is a word that's used to... Read More
Itâ??s easy to understand why you might be drawn to... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Combining Microsoft Business Solutions Great Plains ERP with non-Microsoft Business... Read More
In a previous article, I wrote about OpenOffice... Read More
Just stress testing one of the latest Linux distributions. Been... Read More
Microsoft Great Plains might be considered as ERP platform to... Read More
MicroWorld Technologies, Inc. the leading solutions provider in the area... Read More
When making a decision to buy any piece of software... Read More
Navision Software was purchased by Microsoft and now it is... Read More
eStore Advantage allows front-office applications to communicate with back-office business... Read More
DBxtra is a powerful query and reporting tool that hides... Read More
Microsoft Great Plains is main Microsoft Business Solutions accounting package... Read More
The cornerstone of successful automated office systems is the ability... Read More
This is a short article, written in question/answer/FAQ style to... Read More
I love new technology. I am still ready to wait... Read More
Microsoft Great Plains serves majority of US based horizontal and... Read More
Whether you have used Microsoft Word for years, have just... Read More
Great Plains Integration Manager scripting and translation - overview for... Read More
Microsoft Business Solutions Great Plains has many years of successful... Read More
IBM Lotus Notes with Domino email server is traditional document... Read More
With any good luck and a good amount of hard... Read More
Microsoft CRM is now on the scene and it is... Read More
Just when you thought you were Web savvy, one more... Read More
Case study: A secretary using Corel WordPerfect 7 is often... Read More
Microsoft Great Plains and Microsoft Retail Management System (Microsoft RMS)... Read More
XML Server can be a Web Server that stores the... Read More
Adware is a type of Spyware program that displays some... Read More
The most important benefit of XML is its simplicity. Though... Read More
In this small article we will show you the possible... Read More
Microsoft Great Plains could be tuned and setup to fit... Read More
Microsoft bought Navision, Denmark based software development company, along with... Read More
It is possible that if one avoided all sources of... Read More
Language development computer: Computer-based method for aiding language development seems... Read More
Many Webmasters have never bothered to view their website's server... Read More
ERP Consulting industry is on the way to serve clients... Read More
MS CRM is very close to document workflow automation, including... Read More
Microsoft Great Plains fits to majority of industries, in the... Read More
An integral part of any quality CRM system is lead... Read More
If you are in a business that passes documents around... Read More
Siebel is traditional CRM market leader, however and mostly due... Read More
If you've been using MySQL database to store your important... Read More
Shareware has been fighting the stigma of being misunderstood for... Read More
If you are software developer or database administrator - we... Read More
The fact that a software tester is a most infamous... Read More
Microsoft Business Solutions Great Plains is very popular ERP/MRP applications... Read More
It would be easy to think, like most people apparently... Read More
IBM Lotus Domino or Microsoft Exchange?The severe competition continues for... Read More
A LOT OF UNWANTED FILES.When you uninstall an item of... Read More
So let's begin crunching down these 300 images using Adobe... Read More
I provide, here clear explanations and a count of function... Read More
In the case when you represent mid-size or mid-size-to-large business,... Read More
Following tips help you to learn a software in lesser... Read More
Are you lost in the mess of documents that get... Read More
Are you ready? SQL Server 2005, the next-generation data management... Read More
Software |