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:


CNET

Software vendors woo self-employed workers
CNET
The self-employed worker is becoming increasingly popular among software vendors. And there's a good reason for that. There were 15 million people self-employed in 2015, according to the US Bureau of Labor Statistics. That sum equates to more than 10 ...


New York Post

Corporate America still using Equifax-like software even after hack
New York Post
More than 50,000 organizations are using outdated and leaky versions of Apache, the software whose Struts app gave hackers a back door into Equifax — even though free fixes have been available for nine months, according to Sonatype, a firm that ...
8 best practices for managing software patchesTechRepublic
Don't blame open-source software for poor security practicesThe Mandarin (registration)

all 129 news articles »

Boston.com

Apple's new software, iOS 11, is out — here are 17 major ways your phone just changed
Business Insider
Apple released iOS 11 to everyone with a modern iPhone on Tuesday. You can download it now in Settings > General > Software Update. iOS 11 includes major updates for iPads, including a new desktop-like dock and supercharged multitasking features.
4 things to know about Apple's iOS 11 software updateBoston.com
iOS 11: How to download and install Apple's new iPhone software – and everything else you should do firstThe Independent
Apple just released a major software update for your iPhone and iPad — here's how to install itCNBC
Mirror.co.uk -Quartz -Fortune -Apple Newsroom
all 615 news articles »

WIRED

Software Has a Serious Supply-Chain Security Problem
WIRED
But lately, devious hackers have been targeting their attacks further up the software supply chain, sneaking malware into downloads from even trusted vendors, long before you ever click to install. On Monday, Cisco's Talos security research division ...
Avast! There's malware in that CCleaner software updateArs Technica
Hackers hid malware in CCleaner software - The VergeThe Verge
Hackers compromised free CCleaner software, Avast's Piriform saysReuters
BBC News -Engadget -Sky News -Talos Blog - Cisco Talos
all 196 news articles »

Boston Business Journal

Progress Software pushes back against activist demands
Reuters
(Reuters) - Progress Software Corp (PRGS.O) said on Tuesday that it would not follow activist investor Praesidium Investment Management's demands to replace the chairman of its board or buy a privately held company pitched by the hedge fund. Bedford ...
Progress Software defends chairman Jack Egan from activist investorBoston Business Journal

all 5 news articles »

Chicago Tribune

US government must stop using Kaspersky Lab antivirus software. Should you switch too?
Chicago Tribune
After the U.S. government banned federal agencies from using Kaspersky Lab software last week, worries rippled through the consumer market for antivirus software. Best Buy and Office Depot said they will no longer sell software made by the Russian ...
US Senate votes to ban Kaspersky Lab software from government networksReuters
US Senate votes to oust Russian security software vendor Kaspersky from federal useTechCrunch
US Senate Bans Kaspersky Software Over Suspected FSB TiesThe Moscow Times
Jackson Clarion Ledger -Homeland Security
all 53 news articles »

TNW

Developers are building better software, faster, using AI
TNW
Software engineer Quoc Le grew up in rural Vietnam, in a small village that had no electricity till he was nine. But that didn't stop him eventually becoming a PhD candidate at Stanford in 2007, working out a strategy that would let software learn ...


Network World

5 software licensing challenges in the next generation network
Network World
Every day we hear how the network is changing. Virtualization, Cloud, Software-defined Networking, the Internet of Things — it's clear big transformational change is happening. The technical aspects of these solutions seem to get the most attention ...


Mashable

When paying for antivirus software does more harm than good
Mashable
When it comes to your home computer, you likely don't need paid antivirus software.

and more »

Nasdaq

Progress Software rejects activist investor's demands
Reuters
(Reuters) - Progress Software Corp (PRGS.O) on Tuesday rejected calls by activist investor Praesidium Investment Management to change its acquisition strategy and replace the chairman of its board. Bedford, Massachusetts-based Progress said its ...
Progress Software Says Praesidium Fails To Acknowledge Execution Of StrategyNasdaq
Progress Software Comments on Letter from Praesidium Investment ManagementBusiness Wire (press release)

all 3 news articles »
Google News

Benefits of Shareware

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

Upgrading Great Plains Dexterity Customization ? switching to new technologies: SQL, Crystal, eConne

1. Replace Dexterity cursor with SQL Stored Procedure Dexterity... Read More

Microsoft CRM Integration with Microsoft Retail Management System (RMS) ? Overview

Microsoft Client Relation Management system (Microsoft CRM) and Microsoft RMS... Read More

What is Preventive Maintenance Software?

Preventive Maintenance (PM) is defined as scheduled work done on... Read More

Computer Based Language Development and Spell-checking

Language development computer: Computer-based method for aiding language development seems... Read More

Microsoft Great Plains Multicurrency ? Overview For Implementation Consultant

When you first think about multicurrency ? you probably have... Read More

Making The Decision To Automate Your Software Testing

Not every software testing project can or should be automated.... Read More

Razzle Dazzle Them

Once upon a time not so long ago, there was... Read More

Rapid Application Development - Is it Really a Need of Today?

Rapid Application Development (RAD) is a software development methodology. In... Read More

Microsoft Great Plains RW: Report Writer

Microsoft Business Solutions Great Plains is marketed for mid-size companies... Read More

New Web-Based HR Tool is Max From NAS, Hannibal, and InfoLink

Three highly respected names in Human Resources have joined forces... Read More

Microsoft Great Plains Chemicals & Paint Industry Implementation & Customization Notes

Microsoft Great Plains fits to majority of industries, in the... Read More

Most Common Ways to Accumulate Spyware (where It is Downloaded to Your PC)

It is possible that if one avoided all sources of... Read More

Lowering The Risks In Developing Do-It-Yourself Software Projects

Mike Dunville* had a decision to make. As the new... Read More

About Microsoft Sysprep (System Preparation) Utility (Windows 2000)

The Microsoft system preparation utility for Windows 2000 is a... Read More

Microsoft CRM ? Consulting in the Post-Recession Time

New post-recession era has new features, which didn't exist in... Read More

Crystal Reports For Microsoft Navision - Overview For Programmer/IT Specialist

Let us give you - developer some hints in the... Read More

Microsoft Great Plains Implementation in Russia ? Overview for Consultant

Microsoft Business Solutions Great Plains is very popular ERP platform... Read More

Partitioning, Formatting and Reinstalling in Windows 98

Formatting and reinstalling windows 98 is very easy if you... Read More

The Software 2005 Conference - A Review

The Software 2005 conference is now a wrap. This conference,... Read More

eConnect: eCommerce Development for Microsoft Great Plains

Microsoft Business Solutions Great Plains has several options to enable... Read More

How to Get The Best Accounting Software For Your Small Business

Buying accounting software is a major investment. It's an important... Read More

Software Automation Helps Increase your Bottom Line

When you own a small business, time is money. And... Read More

The Hidden Power of Online Manual

Writing software manuals is boring, isn't it? We often think:... Read More

Groupware and Version History: Collaboration Series #1

This article is the first of a series of articles... Read More