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.

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:

This RSS feed URL is deprecated, please update. New URLs can be found in the footers at https://news.google.com/news

Forbes

Digital Transformation Reveals Limitations Of Software Packages And SaaS
Forbes
Most large enterprises were on a journey for the past 30 years where a higher and higher proportion of the core systems driving the enterprises was software packages or software as a service. Traditional wisdom for companies was “don't build – buy ...

and more »

The Verge

Microsoft alters Surface Hub 2 hardware, delays new software features to 2020
The Verge
Microsoft is unveiling hardware changes to its Surface Hub 2 today. The software giant first unveiled its 50-inch display earlier this year, with a promise of a 2019 release. While the hardware will now be released in Q2 of 2019, many of the cool new ...
Ignite 2018: Microsoft shows off Surface Hub 2 running latest WCOS softwareOnMSFT (blog)
What's next for Surface Hub 2 - Microsoft Devices BlogMicrosoft Devices Blog - Windows BlogWindows Blog

all 54 news articles »

American Software: The SaaS Narrative Collapses
Seeking Alpha
American Software has tried to frame itself as an SaaS company - but 'cloud' sales still represent less than 12% of revenue. A disappointing Q1 undercuts much of the optimism that revenue growth is set to accelerate. After a nearly 40% plunge, AMSWA ...

and more »

TOP500 News

IBM Introduces Software to Alleviate AI Bias
TOP500 News
A cancer treatment recommendation powered by machine learning may be right 98 percent of the time, but what are the liabilities of the software-maker when a misdiagnosis results in death? A less fatal, but still undesirable type of bias comes into play ...

and more »

Threatpost

Adwind RAT Scurries By AV Software With New DDE Variant
Threatpost
The spam campaign features two types of droppers that leverage a new variant to the already-known DDE code-injection attack on Microsoft Excel – enabling them to bypass AV software. DDE is a legitimate method for transferring data between applications ...

and more »

GeekWire

Pixvana's new virtual reality production software lets you edit VR content while wearing a headset
GeekWire
Seattle startup Pixvana today released the full version of its software platform SPIN Studio that helps virtual reality filmmakers edit, process, and deliver video at 8K resolution. Perhaps the most notable update to SPIN is the ability to edit VR ...

and more »

ZDNet

Adwind Trojan circumvents antivirus software to infect your PC
ZDNet
"Although both the generic method and the payload are known, this campaign show[s] how some variance into well-known artifacts can trick antivirus [software]," Cisco Talos says. "Their behavior, however, is clearly classical, which means that ...


CMSWire

Choosing a SaaS Product: 7 Non-Software Related Considerations
CMSWire
When it comes to choosing software, you need to consider more than just the code itself. As more and more companies incorporate software-as-a-service (SaaS) tools into their marketing technology stacks, we felt it was important to address seven vital ...


Mashable

Snag this illustrating software on sale for 50% off
Mashable
Clip Studio Paint Pro is an all-inclusive software meant to make it easier to draw and paint on your computer or tablet. Once you download the software, you'll have access to over 10,000 downloadable brushes as well as a handful of 3D models and figurines.

and more »

China firm opens software development centre in city
The Hindu
Smart device operating system and platform technology provider Thunder Software Technology Co. Ltd. of China has made a foray into India by establishing an IT development centre in Hyderabad. A company that counts US chipmaker Qualcomm as an ...

and more »
Google News

MicroWorld Releases New Version of MailScan Ver. 4.5 - Antivirus and Content Security Software

MicroWorld Technologies, Inc. the leading solutions provider in the area... Read More

Navision Customization: C/SIDE, C/ODBC, C/FRONT, XBRL ? Development Options

Microsoft bought Navision, Denmark based software development company, along with... Read More

Design a Web Album Using Adobe Photoshop- Part 2

So let's begin crunching down these 300 images using Adobe... Read More

DVD Burning Tips

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

Microsoft Great Plains Implementation in Russia ? Overview for Consultant

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

Microsoft Great Plains Accounting/ERP Implementation ? Finance Industry Customization Example

Microsoft Business Solutions Great Plains is very generic accounting application... Read More

Implementing ERP for Large Publicly Traded Corporation ? Microsoft Great Plains

We would like first emphasize the change in the paradigm.... Read More

What You Should Know About Installing Screensavers

Do you remember that frustrating feeling when you find an... Read More

The Top 5 Wrong Reasons For Not Hiring Testers

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

New SQL Delta Version 3.1

COMMAND LINE FUNCTIONA powerful command line script processor has been... Read More

Free Software - Powerful Alternatives to Budget-Busting Software

When you buy a computer, it most likely comes with... Read More

Is Software Tester a Most Infamous Person in a Software Project Team?

The fact that a software tester is a most infamous... Read More

Microsoft Great Plains Implementation for Midsize & Large Corporation: Lockbox Processing

Microsoft Great Plains is now targeting large and midsize businesses... Read More

Protect Your Most Vital Business Asset with Security Software

Homeland security, airport security, Internet security â?" these days weâ??re... Read More

Algebra Help Software

Need help making sense of algebra? Have algebra lectures in... Read More

ERP System of the Future: Database, Business Logic and Interface

We will base our prognosis on our Microsoft Business Solutions... Read More

FreeDOS

Before September 1995, Microsoft Windows was an MS-DOS program. DOS... Read More

How to Tell You Have Spyware, Ad-ware or Viruses

Usually, the easiest way to tell you have spyware is... Read More

Choose your Java Wisely

Java has come along a long way. Many would agree... Read More

Microsoft Great Plains RW: Report Writer

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

Microsoft Great Plains Food Processing ? Implementation & Customization Highlights

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

Quick Summary of Basic and Common Linux Commands

There are many commands that are used in linux on... Read More

RSS: Get Notified When Your Favorite Websites Are Updated

RSS (Really Simple Syndication) is a way for a site... Read More

CRM and Customer Life Cycle

Customer Relationship Management or CRM is a combination of enterprise... Read More

Professional XP Style Icons in Software and Website Development

Using professional icons in your application or website can bring... Read More