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/.
kitchen deep cleaning Deerfield ..Microsoft Business Solutions CRM is present several years on the... Read More
When making a decision to buy any piece of software... Read More
Is Photoshop CS2 worth the upgrade? You bet it is!... Read More
Spyware and malware are large problems for Internet users today... Read More
Microsoft CRM is CRM answer from Microsoft Business Solutions. If... Read More
Follow the steps below to quickly design, generate, and deploy... Read More
Microsoft Business Solutions Great Plains, Navision, Axapta, Solomon and CRM... Read More
This is intermediate level SQL scripting article for DB Administrator,... Read More
Microsoft Great Plains, Navision, Solomon and Axapta are Microsoft Business... Read More
If you company is small or mid-size special products or... Read More
Passwords protect your most sensitive personal, financial and business information.... Read More
Simply put, fleet maintenance allows companies to monitor and maintain... Read More
Sales are all about leverage, because there is only so... Read More
If you've been using MySQL database to store your important... Read More
Microsoft Great Plains has full-featured Manufacturing suite of modules: Capacity... Read More
The US House of Representatives has recently passed the "Spy... Read More
CD and DVD replication is a process that works by... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
If you have been using the Internet for any amount... Read More
User interfaces and accessibility are some of the most important... Read More
After almost two decades of existence, Quark has become the... Read More
There are two approaches for application integration:? Programmer's approach ?... Read More
I started using PIP (Picture It Publishing) Platinum 2002 right... Read More
(1) Avoid using the same variable again and again for... Read More
Looks like Microsoft Great Plains becomes more and more popular,... Read More
interior house cleaning Highland Park ..Have you noticed WordPerfect is gearing up for a comeback... Read More
What is Spyware?Spyware monitors your surfing habits and sends the... Read More
Each Industry and market niche has business specific and unique... Read More
Navision Software was purchased by Microsoft and now it is... Read More
If you have Microsoft Great Plains as main accounting and... Read More
Microsoft Great Plains might be considered as ERP platform to... Read More
If you have Microsoft Great Plains and support it for... Read More
A UNIX Shell is in simplest terms, a command line... Read More
If you company is small or mid-size special products or... Read More
Microsoft Business Solutions Great Plains is mid and even corporate... Read More
Microsoft PowerPoint has dramatically changed the way in which academic... Read More
Language development computer: Computer-based method for aiding language development seems... Read More
Ad-Aware and Spybot are probably the two most well known... Read More
The purpose of one of our projects was MS Exchange... Read More
Microsoft Great Plains fits to majority of industries, in the... Read More
Microsoft CRM customization techniques are very diversified and based on... Read More
It would be easy to think, like most people apparently... Read More
Microsoft Business Solutions CRM is present several years on the... Read More
Microsoft Business Solutions Great Plains, former Great Plains Software Dynamics... Read More
Is Photoshop CS2 worth the upgrade? You bet it is!... Read More
Cyberspace has opened up a new frontier with exciting possibilities... Read More
If you copy something from a Web site or elsewhere...... Read More
Microsoft Business Solutions Great Plains was designed back in the... Read More
Many Webmasters have never bothered to view their website's server... Read More
Microsoft Business Solutions Great Plains has full-featured manufacturing set of... Read More
Software |