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/.
best cleaning company Highland Park ..Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Usually, the easiest way to tell you have spyware is... Read More
Viruses and spyware usually show up on your computer one... Read More
2005 ? Back to the Future.What does the future hold?... Read More
Words we choose to describe things and phenomena often show... Read More
The US House of Representatives has recently passed the "Spy... Read More
This article illustrates the best practices to improve the performance... Read More
Table of Contents1) Introduction 2) Related Work 3) Framework 4)... Read More
Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More
Microsoft released Service Pack 2 (SP2) for the Windows XP... Read More
The java programming language is becoming more and more popular... Read More
In a previous article, I wrote about OpenOffice... Read More
Microsoft Great Plains is one of three Microsoft Business Solutions... Read More
SAP Inc., a global leader in client/server enterprise application software... Read More
Whether you need to close a sale, gather end-user feedback,... Read More
While I was in college, if you would have asked... Read More
When you think... Read More
Document Management or Enterprise Information Management is perhaps one of... Read More
Linux essentials:It's free for download but you have to pay... Read More
Microsoft CRM is new player on the CRM software... Read More
It is possible that if one avoided all sources of... Read More
Microsoft Great Plains serves the wide spectrum of horizontal markets.... Read More
What is a Web Database?A web database is a database... Read More
Former Great Plains Software Dynamics/eEnterprise and currently Microsoft Business Solutions... Read More
We'll give you non formal view, based on our consulting... Read More
high-end home cleaning Winnetka ..A wiki is an editable text-based website. But you don't... Read More
A UNIX Shell is in simplest terms, a command line... Read More
IBM Lotus Notes Domino and Microsoft CRM (Client Relation Management)... Read More
If you have Microsoft Great Plains and support it for... Read More
Language development computer: Computer-based method for aiding language development seems... Read More
If you have Microsoft Great Plains and support it... Read More
We've all seen the ads on TV for Netzero 3G.... Read More
So, why should you use any O/R mapping tool? I... Read More
In our small article we'll consider Microsoft Business Solutions Great... Read More
In this article you will find some background information about... Read More
Are you lost in the mess of documents that get... Read More
How to delete the user? This is the first problem... Read More
Mapping Software Improves Data VisualizationFrom the outset, it is important... Read More
If you are to buy a HelpDesk & Asset Management... Read More
Does Microsoft Have any Real Competition? Copyright (c) 2003 Gregory... Read More
Should one use Windows Update?This topic has good and valid... Read More
NOTE: Please take time to read on - it may... Read More
Accounts payable is just one area of office management where... Read More
Security flaws have long plagued Internet Explorer (IE), the market-dominating... Read More
RSS (Really Simple Syndication) is a way for a site... Read More
... Read More
In the real world a "fire wall" is a fireproof... Read More
Microsoft SQL Server is the leader for inexpensive and middle... Read More
Microsoft CRM is CRM answer from Microsoft and attempt to... Read More
We were recently faced with a decision: either to let... Read More
Software |