Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/a26f9f83/public_html/articles/includes/config.php on line 159
C++ Function Templates > NetSparsh - Viral Content you Love & Share

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.


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


More articles can be found at

In The News:

This RSS feed URL is deprecated, please update. New URLs can be found in the footers at


Jaguar Land Rover vehicles to use BlackBerry software
(Reuters) - Luxury carmaker Jaguar Land Rover will use BlackBerry Ltd's infotainment and security software in its vehicles, the companies said on Thursday. FILE PHOTO: A Blackberry sign is seen in front of their offices on the day of their annual ...
BlackBerry signs in-car software deal with Jaguar Land RoverFinancial Post
BlackBerry to provide software for Jaguar Land Rover vehiclesNasdaq

all 59 news articles »

The Register

F-35B Block 4 software upgrades will cost Britain £345m • The ...
The Register
Britain will spend £345m ($486m) upgrading its F-35B fighter jets to the most recent, combat-ready, version of the aircraft's operating system. The figure was indirectly revealed by defence procurement minister Guto Bebb, in response to a Parliamentary ...

and more »

Everybody loves Microsoft's open switch software, SONiC • The ...
The Register
ROUNDUP This week's networking news roundup isn't only “what happened at the Open Compute Project summit?” – there's also news from F5, Palo Alto Networks, and Dell EMC. Microsoft coordinated a bunch of announcements at the Open Compute Project summit ...

and more »


Salesforce's pricey MuleSoft deal could force rivals to pay up for cloud software companies
Salesforce's $6.5 billion acquisition of MuleSoft was the most expensive cloud software deal in history, according to some analysts, and could raise the price for future transactions. According to a report on Wednesday from analysts at JMP Securities ...
Exclusive: Salesforce in advanced talks to buy MuleSoft - sourcesReuters
Salesforce Signs Definitive Agreement to Acquire MuleSoftPR Newswire
Financial Newsletter - ZacksZacks
all 231 news articles »

The Register

AI software that can reproduce like a living thing? Yup, boffins have only gone and done it
The Register
A pair of computer scientists have created a neural network that can self-replicate. “Self-replication is a key aspect of biological life that has been largely overlooked in Artificial Intelligence systems,” they argue in a paper popped onto arXiv this ...


Alphabet Company Launches Free VPN Software for Windows and ...
Jigsaw, the Alphabet-owned firm formerly known as Google Ideas, has announced a new free VPN (virtual private network) software package called Outline. The new open source VPN serves as a way to protect journalists, activists, and others who need a ...
Google is hiring software engineers: Know how to applyIndia Today

all 108 news articles »


Sketchfab lets you import 3D models into your favorite 3D software
Sketchfab has built a huge library of 3D models. Some of them are available under a Creative Common license, others can be purchased. The startup now wants to help you do stuff with all this content. You can now download and import 3D models into your ...


Hackers hit software supply chains more in 2017 than prior two years combined
Hackers dramatically increased the number and scope of attacks on software manufacturers as a way to attack user machines in 2017, according to a new report by Symantec. "When you look at the numbers, it's no longer one off attacks," Kevin Haley ...

and more »


Life Fitness launches cloud gym management software | ZDNet
Life Fitness, best known for gym equipment, is getting into the software business via a cloud facility management system called the Halo Fitness Cloud. The effort comes as fitness players are increasingly connecting gym experiences to virtual training ...

and more »

Gulf Coast Collection Bureau Partners with Divinity Software Group to Provide New Cutting-Edge Technology Solutions
JACKSONVILLE, Fla. -- Jack Brown III, the President of Gulf Coast Collection Bureau (GCCB) and ACA International, Inc.'s President-Elect has chosen Divinity Software Group as a partner to provide new cutting-edge technologies in automating daily ...

Google News

Microsoft Great Plains Food Processing ? Implementation & Customization Highlights

Microsoft Great Plains might be considered as ERP platform to... Read More

S is for Spying, Surveillance -- and for Software as Well

Words we choose to describe things and phenomena often show... Read More

Microsoft Great Plains Upgrade ? Things to Consider and FAQ

If you have Microsoft Great Plains and support it for... Read More

7 Things to Consider Before Buying Small Business Accounting Software

The world of small business accounting software can be a... Read More

OS 101: Suggestions for Choosing an Operating System

An operating system (abbreviated OS) is essentially the path through... Read More

15 Questions to Ask Your Software Vendor

When making a decision to buy any piece of software... Read More

Is Your Computer Sick?

Viruses and spyware usually show up on your computer one... Read More

The Bluebird Project

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

Secure File Transfer Using SSH Plus Additional Audit & Automation - FSA Reporting

In order to meet regulatory and corporate compliance requirements reporting... Read More

Best Practices In Choosing Network Monitoring Software

Blue Cross and Blue Shield of Hawaii (HMSA) found itself... Read More

DVD Burning Tips

CD and DVD replication is a process that works by... Read More

The Importance of Timely Timesheets

Whether you are a small consultancy firm, a medium sized... Read More

20 Extra Hours Per Week: What Would You Do?

While I was in college, if you would have asked... Read More

String in Java

Handling character strings in Java is supported through two final... Read More

Off The Record - Tips For Picking Recording Software

Need software to record your voice, streaming audio or musical... Read More

Microsoft CRM Implementation for Large Corporation ? overview

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

Groupware Finds Home in American Business: Collaboration Tool Analysis

What is Groupware?Have you ever had to manage document collaboration... Read More

Save Your Resources - Combine Your IM Clients

There are so many different programs that clutter up your... Read More

Why Stick With Email Clients Like Outlook?

Trying to figure out a stream in banning one email... Read More

Benefits of Shareware

Shareware has been fighting the stigma of being misunderstood for... Read More

Free Preventive Maintenance Software

While several preventive maintenance software manufacturers offer free trials for... Read More

Integrating Microsoft Great Plains Accounting/ERP: RMS, CRM, eCommerce, Lotus Domino ? overview

Microsoft Business Solutions Great Plains has substantial market share among... Read More

Microsoft CRM Customization

Microsoft CRM customization techniques are very diversified and based on... Read More

Microsoft Great Plains Integration Manager ? Working With Text File

Microsoft Business Solutions main middle market ERP application - Microsoft... Read More

Screenshots Vista Windows

Features Additionally, Vista will include many other new features.Aero Vista... Read More