Software Information

C++ Function Templates


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/.


MORE RESOURCES:

Software allows social sites to share info
Bizjournals.com, NC - 14 hours ago
JACKSONVILLE — A Jacksonville software company is knocking down walls between social networks, such as MySpace and LinkedIn, by giving users more control ...


SYS-CON Media

Composite Software to Exhibit at SYS-CON's Virtualization and ...
SYS-CON Media, NJ - 6 hours ago
SYS-CON Events announced today that the eading global virtualization technology provider Composite Software to exhibit at SYS-CON's 4th International ...
Active Endpoints Named "Gold Sponsor" of SYS-CON's SOA World ... SYS-CON Brasil (Assinatura)
EMC Named "Gold Sponsor" of SYS-CON's SOA World and Virtualization ... SYS-CON Media
Intel Named "Gold Sponsor" of SYS-CON's SOA World and ... SYS-CON Media
all 35 news articles


Software firms defy the gloom
Times Online, UK - 7 hours ago
SCOTLAND’S software sector is defying economic gloom to attract millions of pounds in new business, according to a new survey. ...


Review: Tiny flash drives improve their security
San Jose Mercury News,  USA - 22 hours ago
I put a few secure flash drive solutions to the test: Take Anywhere's Pocket Safe ($59.95), the IronKey ($149) and TrueCrypt, a free software program that ...


Times Online

Orange suspends Blackberry Bold handsets over software glitches
Telegraph.co.uk, United Kingdom - Oct 10, 2008
By Matthew Moore Phone provider Orange has stopped dispatching Blackberry Bold handsets due to software problems with the devices, in a move that threatens ...
Software problems hit BlackBerry Bold IT PRO
Third-party BlackBerry Software Development a Learning Process for RIM Brighthand
Orange UK Suspends Sales of BlackBerry Bold Due to Software Bugs Cellular-News
CRN - U.S. News & World Report
all 1,068 news articles


Sys-Con Italia (Comunicati Stampa)

AppSense Named "Gold Sponsor" of SYS-CON's Virtualization & Cloud ...
Sys-Con Italia (Comunicati Stampa) - 9 hours ago
... Black Duck Software, Blackbaud, Blade Network Technologies, Blue Coat, Blue Lane, BlueArc, BlueNote Networks, BluePheonix Solutions, BMC Software, ...


40 laid off at software company as execs arrested
nwitimes.com, IN - 15 hours ago
SEATTLE - Two former software executives grossly overstated their company's revenue to attract more than $50 million in private investment, prosecutors said ...
Prosecutor: Exec used company as 'piggy bank' Seattle Times
Judge orders Entellium CEO kept in custody Seattle Post Intelligencer
Feds can’t find $9.6M from Entellium Bizjournals.com
all 20 news articles


BBC News

US judge halts sales of RealNetworks DVD software
Reuters - Oct 9, 2008
RealDVD, a software product from RealNetworks subsidiary RealNetworks Home Entertainment Inc, allows users to create a copy of a DVD for their computer's ...
Judge Temporarily Halts Sales of RealNetworks Copying Software Wall Street Journal
For PC: RealDVD works so well that it's on legal hold Dallas Morning News
Drop that DVD copying software and put your hands in the air InfoWorld
Wired News - International Herald Tribune
all 335 news articles


Dorado Software Announces Redcell Storage Commander for Automated ...
MarketWatch - Oct 10, 2008
Redcell Storage Commander, Redcell OpsCenter, and Redcell Network Commander are generally available and trial software is available on the Dorado Software's ...


The ONLY Property and Facilities Management Software Exhibition in ...
1888 Press Release (press release), TX - 2 hours ago
The three leading software solutions who are at the forefront of technology exhibiting under one roof at the only property and facilities management ...

Software - Google News

home | site map
© 2006