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/.
Wood Dale Chicago prom limo .. Lockport Chicago limo O’HareSince technology changes so quickly, it is hard to begin... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Mike Dunville* had a decision to make. As the new... Read More
There are so many different programs that clutter up your... Read More
Microsoft Business Solutions Great Plains, Solomon, Navision, Axapta, Microsoft CRM... Read More
You might think you don't need a firewall... Read More
CD and DVD replication is a process that works by... Read More
Microsoft Business Solutions CRM and IBM Lotus Notes Domino, being... Read More
Microsoft Outlook is one of the most widely used software... Read More
You would like to protect your documents, wouldn't you? Reasons... Read More
Microsoft Business Solutions main middle market ERP application - Microsoft... Read More
Shareware is software that you can try before you buy;... Read More
Microsoft CRM is winning market share step-by-step from such the... Read More
Microsoft Great Plains is one of the Microsoft Business Solutions... Read More
How would you like to prevent spyware and adware from... Read More
Spyware, what it is and what it does. Basically, spyware... Read More
This is a short article, written in question/answer/FAQ style to... Read More
This article will not attempt to advocate the use of... Read More
Microsoft Business Solutions Great Plains is very popular ERP platform... Read More
Microsoft Business Solutions Great Plains has Project Accounting module where... Read More
This article is for advanced Microsoft CRM SDK C# developers.... Read More
Spyware and malware are large problems for Internet users today... Read More
Former Great Plains Software Dynamics/eEnterprise and currently Microsoft Business Solutions... Read More
The Windows Indexing Service provides you with the ability to... Read More
When Windows fails to boot it is normally caused by... Read More
Antigo wedding limo ..Ok... Where to start?Well, I guess I will start at... Read More
Follow the steps below to quickly design, generate, and deploy... Read More
Spyware is software or hardware installed on a computer without... Read More
Microsoft Business Solutions Great Plains, former Great Plains Software Dynamics... Read More
Every organization which creates collaborative documents, whether they are budgets,... Read More
Spyware, what it is and what it does. Basically, spyware... Read More
Microsoft Great Plains serves majority of US based horizontal and... Read More
Microsoft Great Plains, former Great Plains Software Dynamics / eEnterprise... Read More
Ad-Aware and Spybot are probably the two most well known... Read More
Microsoft Business Solutions Great Plains has full-featured manufacturing set of... Read More
We all already got used to computer monitoring both at... Read More
After seeing many people complain about their weak Internet security... Read More
Formatting and reinstalling windows 98 is very easy if you... Read More
Words we choose to describe things and phenomena often show... Read More
The fact that a software tester is a most infamous... Read More
If you are in a business that passes documents around... Read More
After almost two decades of existence, Quark has become the... Read More
If you copy something from a Web site or elsewhere...... Read More
The software giants don't do everything and don't always produce... Read More
So, why should you use any O/R mapping tool? I... Read More
Disclaimer: All the thoughts expressed are my views only! Your... Read More
In linux, one of great commands for finding out information... Read More
Customer Relationship Management, abbreviated "CRM," is the term for a... Read More
It???s easy to understand why you might be drawn to... Read More
Task Manager is a Windows system utility that displays thetasks... Read More
Software |