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:

Fortune

Oracle Broadens Reach in Web-Based Software
Wall Street Journal
By BEN WORTHEN Oracle Corp. agreed to acquire online-software maker Taleo Corp. for $1.9 billion, its second such acquisition in recent months and the latest sign that the software industry's old guard is embracing a newer model.
Oracle Will Purchase Taleo for $46 a Share in Deal Valued at $1.9 BillionBloomberg
Oracle Embraces the Cloud With $1.9 Billion Taleo DealNew York Times
Oracle paying $1.9 billion for Taleo's HR softwareSan Francisco Chronicle
Forbes -San Jose Mercury News
all 451 news articles »


Tableau Software Positioned as "Challenger" in Leading Analyst Firm Magic ...
San Francisco Chronicle (press release)
Tableau® Software, the global leader in rapid-fire business intelligence software, today announced that Gartner, Inc. has positioned Tableau as a "Challenger" in its 2012 Magic Quadrant for Business Intelligence Platforms* report.

and more »


ND software <3794.OS>-2011/12 div forecast
Reuters
Feb 10 (Reuters) - ND SOFTWARE CO LTD PARENT-ONLY EARNINGS ESTIMATES (in billions of yen unless specified) Full year to Full year to March 31,2012 March 31,2012 LATEST PREVIOUS FORECAST FORECAST Annual div 45.00 yen 30.00 yen -Q2 div nil nil NOTE - ND ...

and more »


GrayHair Software releases a dynamic reporting engine to complement its cloud ...
EON: Enhanced Online News (press release)
MOUNT LAUREL, NJ--(EON: Enhanced Online News)--GrayHair Software, Inc., a leader in solutions for business mailers, announces the release of a ground-breaking reporting engine that expands the versatility and depth of the services offered in its ...

and more »


Software maker Nuance shares plunge on 1Q miss
CBS News
Software maker Nuance Communications Inc. reported late Thursday that its first-quarter net income rose as revenue climbed for its health care, mobile and imaging applications, but its adjusted earnings and revenue fell short of analysts' expectations ...
Nuance Blunt In Earnings MissInvestor's Business Daily

all 31 news articles »


Software Developers Get SaaS-y with Expanded Software-as-a-Service Platform
EON: Enhanced Online News (press release)
The software distribution platform allows software publishers to put their applications immediately in the cloud as a Software-as-a-Service (SaaS) offering with no development or rewrite effort. “Porting and conversion costs have been eliminated, ...

and more »


Home Media Magazine

Video-Game Retail Sales in US Slid 34% in January, NPD Reports
BusinessWeek
9 (Bloomberg) -- US retail sales of video-game hardware, software and accessories tumbled 34 percent in January to $750.6 million from a year earlier, researcher NPD Group Inc. said today in an e-mailed statement. Sales a year ago were $1.14 billion, ...
Videogame Sales Plunge 34 Percent in JanuaryPC Magazine
US video game sales fall 34 percent in January due to lack of new titlesWashington Post

all 111 news articles »


Callidus Software Increases Sales but Misses Estimates on Earnings
DailyFinance
By Seth Jayson, The Motley Fool Posted 11:19AM 02/09/12 Investing Callidus Software (NAS: CALD) reported earnings on Feb. 8. Here are the numbers you need to know. For the quarter ended Dec. 31 (Q4), Callidus Software beat slightly on revenues and ...
Callidus Software to Participate in Upcoming Investor ConferencesMarketWatch (press release)
Callidus Software Slips To Wider Q4 LossNASDAQ
Callidus Software Announces Fourth Quarter and Full Year 2011 ResultsMarketwire (press release)

all 27 news articles »


Raytheon Releases Software Upgrade for Airborne Communication System
MarketWatch (press release)
The enhancement is provided by an upgrade to Integrated Waveform (IW) software, following successful field tests hosted by the Defense Information Systems Agency (DISA). Prior to the software upgrade, this form of radio communications had limited ...

and more »


Business Insider

Symphony, Teleca merge to tap corporate mobile mkt
Reuters
* New firm sees 2012 revenues of $350 milllion * To tap surging mobile enterprise software market Feb 9 (Reuters) - Symphony Technology Group, the private equity firm of billionaire Romesh Wadhwani, is merging its business software and mobile services ...
Symphony Services and Teleca Merge Creating World's Leading Services ...EON: Enhanced Online News (press release)
Symphony, Teleca merge to tap corporate mobile marketReuters UK
Symphony Services, Teleca to mergeSan Jose Business Journal

all 16 news articles »

Google News

Home | Site Map

Powered By: Free Work At Home Business Opportunity!

© 2006