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

In The News:


Recode

We need a software revolution for the greater social good
Recode
Five years ago, tech entrepreneur and venture capitalist Marc Andreessen famously wrote, “Software is eating the world.” It's hard to think of more prophetic words coming out of Silicon Valley, and new players that have software at their core continue ...


CBS Local

Denver Admits Software Licensing Violations « CBS Denver
CBS Local
Denver taxpayers will be paying millions of dollars more this year for city use of Oracle software after the city admitted it violated its licensing agreements with ...

and more »

TheStreet.com

Adobe is Piecing Together Different Parts of Its Marketing Software Empire
TheStreet.com
For example, Adobe's mainstay Creative Cloud provides a common layer of cloud services, management software and developer tools for its content-creation apps, and the company has built integrated workflows for Creative Cloud apps and its Marketing ...
Adobe, Microsoft Join Forces In Sales, Marketing SoftwareInvestor's Business Daily
Adobe Overhauls Digital Marketing Services With an Eye on Non-MarketersFortune
Adobe Launches Adobe Experience Cloud | Adobe NewsroomAdobe Newsroom
Adobe Newsroom
all 129 news articles »

Sighthound's AI Software Now Reads License Plates
Yahoo Finance
PALO ALTO, Calif., March 23, 2017 (GLOBE NEWSWIRE) -- Sighthound, Inc. today announced the release of a deeply learned license plate detection and recognition API in its Cloud service and as part of its Sighthound Sentry software. The software scans ...

and more »

MIT Technology Review

A Fight Over Tractors in America's Heartland Comes Down to Software
MIT Technology Review
But when the day finally comes that something breaks down and a repair is needed, they are likely to get a rude awakening: They can't fix it themselves. At least, not without buying hacked software on the black market that could violate the company's ...
Tractor Hack: Farmers are harnessing hacked software for John Deere repairsFox News
Farmers are pirating John Deere tractor software to stick it to the ...ExtremeTech
Tractor hackers! Farmers turn to black market for illicit John Deere software needed to repair their equipment and ...Daily Mail
Motherboard - Vice
all 17 news articles »

Engineer: 'Software eating the world'
The Daily Advance
Mike Casavant, engineering manager for Dell EMC, discusses cloud computing and other services his company provides during the first-ever Regional Technology Showcase at the Ridley Student Center at Elizabeth City State University, Wednesday.

and more »

Forbes

How To Evaluate Whether Your Business Needs A Custom Software Solution
Forbes
Having run a custom software service shop for a few years, I can tell you, going custom isn't the right answer for every situation. While I'm no longer running that business, I do have a team of engineers on staff, so I debate between custom or off-the ...


Network World

Cisco closes AppDynamics deal, increases software weight
Network World
Cisco today closed its approximately $3.7 billion deal for application analytics specialist AppDynamics giving the networking giant a nice revenue stream and bolstering its software strategy. The nine-year-old company – which Cisco bought Jan. 24, days ...
Cisco Closes $3.7 Billion Acquisition Of AppDynamics To Build The 'Next Software Franchise'CRN
SEC FORM 4SEC
SEC FORM 4 - SEC.govSEC.gov

all 161 news articles »

Chicago Tribune

Why software engineers at Tock hang out in top restaurant kitchens
Chicago Tribune
Why would a software programmer shadow a server in a fancy restaurant? Or hang out in the kitchen where they orchestrate the orders between the chefs and the waiters? It's how Brian Fitzpatrick, co-founder and chief technology officer at restaurant ...


Facebook live videos take aim at Twitch with streaming software, hardware support
PCWorld
Alongside news that live videos can now be shot on standard PCs, not just mobile devices, Facebook also revealed today that personal accounts are now able to live stream videos funneled through streaming software or external streaming hardware.
Girl, 15, found safe after disturbing video of her seen on Facebook LiveWGN-TV
New Ways to Go Live: Now From Your Computer | Facebook NewsroomFacebook Newsroom
Facebook Live assault of girl, 15: 'No one human deserves what happened to her'Chicago Tribune

all 294 news articles »
Google News

Ukraine IT Myths Dispersed

While Ukraine is becoming a new popular IT outsourcing destination,... Read More

Microsoft RMS ? Great Plains Integration ? Overview For IT Specialist

Microsoft Great Plains and Microsoft Retail Management System (Microsoft RMS)... Read More

Fundraising Software ? How Can That Help Me?

Fundraising software lets you connect with donors in a way... Read More

Selecting Corporate ERP: Microsoft Business Solutions Great Plains ? Estimation Parameters

As we could imagine, if you are reading this article... Read More

The Bluebird Project

The objective for Zandi Digital is to make available clever... Read More

SQL Administrator Skills Required to Support Microsoft Great Plains

Microsoft Great Plains is becoming more and more popular and... Read More

Document Templates Give You The Perfect Framework For Your Documents

When it comes to running an office, the SOHO entrepreneur... Read More

What to Do if All Screensavers Fun is Grayed Out?

Finally, you have some time to personalize your desktop with... Read More

Microsoft CRM Implementation for Large Corporation ? overview

Microsoft Business Solutions CRM is now approaching the phase of... Read More

How to Build a Basic CSS Layout

Designing without tables by using CSS layouts is fast becoming... Read More

ERP Consulting: Microsoft Great Plains Partner Future Directions

In the Clinton era the status quo was simple: you... Read More

Inherent Dangers Of File Sharing Via The Internet.

Cyberspace has opened up a new frontier with exciting possibilities... Read More

Microsoft CRM USA Nationwide Remote Support

Remember old good days when your company probably had Great... Read More

Microsoft Navision Customization Upgrade ? Tips For Programmer/IT Specialist

Currently Microsoft Business Solutions is on the way of creating... Read More

The Top 5 Wrong Reasons For Not Hiring Testers

Considering whether or not your software company should hire a... Read More

Software Companies: Generate New Revenue Streams and Decrease Costs with Custom e-Learning Content

It's no secret that software companies operate in a very... Read More

Best Spyware Removers

Finding the best spyware removers to detect and remove spyware... Read More

netstat - Linux Command to Display Networking Information

In linux, one of great commands for finding out information... Read More

Huddle Up; Groupware on Three

It could just be me, but my experiences with document... Read More

Spyware - The Internet Devil Of Our Times!

Spyware and Adware infest over 90 percent of computers in... Read More

Should I Use Windows Update?

Should one use Windows Update?This topic has good and valid... Read More

Accounts Payable: A Powerful Document Management and Workflow Solution

Accounts payable is just one area of office management where... Read More

Microsoft Great Plains customization ? Freight Forwarding/Transportation industry example

Microsoft Business Solutions Great Plains version 8.5, 8.0, 7.5, Great... Read More

.NET :Solving the Multiple Inheritance Issue Under .NET Platform

.NET platform does not support multiple inheritance. Do not confuse... Read More

SpyBot - Search and Destroy versus Microsoft AntiSpyware (Beta).... My personal experiences...

Ok... Where to start?Well, I guess I will start at... Read More