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/.
![]() |
|
![]() |
|
![]() |
|
![]() |
Microsoft Business Solutions Great Plains, former Great Plains Software Dynamics... Read More
GroupwareEfforts are continually made to manage the unavoidable ad hoc... Read More
One of the things we can be as certain of... Read More
Microsoft Business Solutions Great Plains is written in Great Plains... Read More
Best Software Act! is very popular CRM for small and... Read More
Microsoft Business Solutions Great Plains was historically designed to serve... Read More
In this small article we will be looking at the... Read More
Homeland security, airport security, Internet security â?" these days weâ??re... Read More
In this short FAQ style article we would like to... Read More
Microsoft Business Solutions Great Plains was designed back in the... Read More
Bill of Lading is required report for Logistics and Freight... Read More
Microsoft Business Solutions offers several ERP applications: Great Plains, Navision,... Read More
While I was in college, if you would have asked... Read More
Usually, the easiest way to tell you have spyware is... Read More
eStore Advantage allows front-office applications to communicate with back-office business... Read More
Let us give you - developer some hints in the... Read More
This article is for advanced Microsoft CRM SDK C# developers.... Read More
There is many things more frustrating than surfing a website... Read More
Disclaimer: All the thoughts expressed are my views only! Your... Read More
Before being able to choose a secure Internet communication system,... Read More
The java programming language is becoming more and more popular... Read More
Around the same time Microsoft made its move with .Net... Read More
When making a decision to buy any piece of software... Read More
You turn on your computer, and it doesn't look quite... Read More
Microsoft Business Solutions CRM proved to be reliable solution in... Read More
Microsoft Great Plains and Microsoft Retail Management System (Microsoft RMS)... Read More
Microsoft CRM customization techniques are very diversified and based on... Read More
In the new era of internet marketing the problem of... Read More
Passwords protect your most sensitive personal, financial and business information.... Read More
With so many Microsoft Windows related viruses, errors, and other... Read More
Itâ??s easy to understand why you might be drawn to... Read More
Great Plains Accounting, accounting package for mid-size and small companies... Read More
With thousands of web pages added to the Net every... Read More
As seeing large number of implementations ? in our case... Read More
Just imagine: you are walking, say, towards your car, and... Read More
Following tips help you to learn a software in lesser... Read More
In the case when you represent mid-size or mid-size-to-large business,... Read More
Currently Microsoft Business Solutions is on the way of creating... Read More
There are plenty of articles out there about how to... Read More
Mike Dunville* had a decision to make. As the new... Read More
Microsoft Great Plains may be recommended for international freight forwarding... Read More
You would like to protect your documents, wouldn't you? Reasons... Read More
Fortunately one of the most common reasons cited for the... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
We will base our prognosis on our Microsoft Business Solutions... Read More
It's not very often I get excited about a software... Read More
Sticky Noteshttp://www.deprice.com/stickynote.htmWith StickyNote 9.0, you can create beautiful 3D notes... Read More
OEComplete is a utility for managing the personal information of... Read More
The various resume software offered, particularly on the internet, can... Read More
Spyware is like the new technological nuclear weapon on the... Read More
With the advent of 'Service Pack 2' for Windows XP... Read More
Creating a new markup language.Introduction.General Reuse Markup Langauge, or GRML,... Read More
IntroductionPHP can be used for a lot of different things,... Read More
There are so many different programs that clutter up your... Read More
Since Version 8.0 Microsoft Business Solutions Great Plains & Great... Read More
Your computer cost you from hundreds to thousands of dollars,... Read More
This short paper will expand on two key reasons to... Read More
To all web designers out there, this article is for... Read More
Microsoft Great Plains and Microsoft CRM become more and more... Read More
Microsoft Business Solutions Great Plains, former Great Plains Software eEnterprise,... Read More
This article is the first of a series of articles... Read More
If you look back to the history, you will see... Read More
Former Great Plains Software Dynamics/eEnterprise and currently Microsoft Business Solutions... Read More
What is Tripwire?Tripwire is a form intrusion detection system (IDS)... Read More
FTP stands for "file transfer protocol". FTP is basically a... Read More
Microsoft released Service Pack 2 (SP2) for the Windows XP... Read More
Software |