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/.
Lincoln Stretch rentals Alsip ..TCO (Total Cost Ownership) is the buzzword in... Read More
Around the same time Microsoft made its move with .Net... Read More
Almost all new and major brand of PCs come with... Read More
Several software companies design programs for preventive maintenance. Most of... Read More
IntroductionDuring the early years of our modern computer era, very... Read More
In our opinion, traditional approach when you select ERP/MRP system... Read More
Microsoft CRM has built-in conversion tool, however you should probably... Read More
What is 'adware'?Adware is basically software or scripts that are... Read More
We've all seen the ads on TV for Netzero 3G.... Read More
Microsoft Business Solutions CRM and IBM Lotus Notes Domino, being... Read More
Not every software testing project can or should be automated.... Read More
Microsoft Client Relation Management system (Microsoft CRM) and Microsoft RMS... Read More
Microsoft Business Solutions Great Plains has substantial market share among... Read More
Formatting and reinstalling windows 98 is very easy if you... Read More
In the early days of the personal computer, we're talking... Read More
ERP Consulting industry is on the way to serve clients... Read More
No matter how much you enjoy your favorite screensavers, sometimes... Read More
Using professional icons in your application or website can bring... Read More
You might think you don't need a firewall... Read More
We were recently faced with a decision: either to let... Read More
Microsoft Business Solutions ? Great Plains is designed to meet... Read More
I have yet to see a business that, sometimes in... Read More
As Mozilla Firefox nears 10% market share, with well over... Read More
Introduction To ISDN, Part III: Configuring PPP PAP AuthenticationNow we... Read More
The objective for Zandi Digital is to make available clever... Read More
Western Springs shuttle limo ..Usually workflow & messaging is realized in CRM and then... Read More
Professional services firm cuts costs and improves productivity with integrated... Read More
Security flaws have long plagued Internet Explorer (IE), the market-dominating... Read More
Let's first look at your ERP system selection (without Retail... Read More
Innovative Maintenance Systems (IMS) is one company that offers solutions... Read More
When you first think about multicurrency ? you probably have... Read More
Microsoft Business Solutions is now in process of creating so... Read More
In linux, one of great commands for finding out information... Read More
In our opinion, traditional approach when you select ERP/MRP system... Read More
In a previous article, I wrote about OpenOffice... Read More
Mapping Software Improves Data VisualizationFrom the outset, it is important... Read More
We'll give you non formal view, based on our consulting... Read More
It could just be me, but my experiences with document... Read More
At the end of XX century, in the late 1990th... Read More
Stop the Runaway MouseWhat's the "runaway mouse?" You've seen it...you... Read More
This article illustrates the best practices to improve the performance... Read More
Intro This concise article will tell you in plain English... Read More
Microsoft CRM customization techniques are very diversified and based on... Read More
Microsoft Business Solutions Great Plains has substantial market share among... Read More
Microsoft Great Plains could be tuned and setup to fit... Read More
Programming Help for BeginnersWe write programs to instruct computers. When... Read More
The Internet is reshaping every form of communications medium, and... Read More
Microsoft CRM is new player on the CRM software... Read More
Manufacturing in the USA is far away down from mid... Read More
The java programming language is becoming more and more popular... Read More
Software |