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

59% Of Deloitte's 2017 Technology Fast 500 Are Software Companies
Forbes
Seven of the top ten winners are software companies including ClassPass, Toast, Liftoff, SalesLoft, ShipHawk,iSpot.tv and Cylance. The following graphic lists the top ten Deloitte 2017 North America Technology Fast 500 winners. Communications, software ...


Axios

Software startups raise billions behind closed doors
Axios
Software startups raise billions behind closed doors. Courtesy Barron's. "When a venture capitalist coined the concept 'unicorn club' in 2013, it referred to software start-ups valued at $1 billion or more — just 39 at that time. ... Today, Dow Jones ...


NPR

Government Outlines When It Will Disclose Or Exploit Software ...
NPR
The Trump administration put out guidelines for how the government will decide what software vulnerabilities it will share with tech companies to fix, and which ...

and more »

Fast Company

The Washington Post Is A Software Company Now
Fast Company
Though selling software as a service sounds like a quintessential story of the Post's Jeff Bezos era, Arc's origins date to the period before he bought the paper. A half decade or so ago, like every other major newspaper publisher in America, the ...


Forbes

Startup ThoughtSpot's $160M Aimed At Tableau Software In $70B Market
Forbes
In the last month, I've talked to two startups -- Santa Cruz-based Looker (which I wrote about in October) and Palo Alto's ThoughtSpot (whose CEO Ajeet Singh I interviewed on November 10) -- which between them have raised a whopping $340 million to ...


Holcomb seeks tax tweak for software firms
Indianapolis Business Journal
Holcomb will ask lawmakers to exempt cloud- or subscription-based software—often called software as a service—from Indiana's 7 percent sales tax. The move would save money for nearly every company in the state and could bolster local firms that sell ...

and more »

PetaPixel (blog)

Sony Launches Imaging Edge Software Suite: Remote, Viewer, and Edit
PetaPixel (blog)
Sony has just announced its Imaging Edge software suite. The suite comes with three key pieces of software: 'Remote', 'Viewer', and 'Edit'. Sony say it will speed up workflow and allow users to “unleash the full potential” of the Pixel Shift technology ...


WCNC

New software may burn out older iPhones
WCNC
"What's really happening is the software updates," said Tishin Donkersley. You may have notices that a new operating system is released near the release date of a new iPhone. And Donkersely says new software can be tougher for your phone's older ...

and more »

TechCrunch

Facebook open sources Open/R distributed networking software ...
TechCrunch
Facebook is no stranger when it comes to open sourcing its computing knowledge. Over the years, it has consistently created software and hardware internally,..

and more »

Economic Times

IBS Software sets up unit in SmartCity Kochi
Economic Times
KOCHI: IBS Software (IBS), a leading technology provider to the global travel, transportation and logistics industry has expanded its operation to SmartCity Kochi. The newly-opened facility at SmartCity Kochi covers an area of 27, 489 sq. ft. IBS ...

and more »
Google News

Four Desktop Tools To Drastically Increase Your Productivity

Sticky Noteshttp://www.deprice.com/stickynote.htmWith StickyNote 9.0, you can create beautiful 3D notes... Read More

What You Must Know About Spyware Right Now

Spyware is like the new technological nuclear weapon on the... Read More

Change Your Word Docs in Record Time!

Anyone who has ever used Microsoft Word knows that it... Read More

Free Software for Newbies and Web Developers

Here is some free software tools to help you build... Read More

Microsoft Great Plains e-Commerce ? additional considerations for programmer

Microsoft Great Plains, designed back in 1990th as database transferable... Read More

How to Build a Basic CSS Layout

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

Microsoft Great Plains - Typical Problems And Fixes ? Overview For IT Administrators

How to delete the user? This is the first problem... Read More

Managing Stress in the Computer Industry - Five Steps to a Stress-free Life

It would be easy to think, like most people apparently... Read More

IT Strategy for Large Corporation: ERP/MRP/CRM, Unix/Linux/Windows, Microsoft/Java

Combining Microsoft Business Solutions Great Plains ERP with non-Microsoft Business... Read More

5 Time-Saving Tips in Microsoft Word

Whether you have used Microsoft Word for years, have just... Read More

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

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

Groupware: What Works the Way Businesses Do?

GroupwareThe internet is full of 1.5 million to 7 million... Read More

Spyware, Adware, etc. -- Terms and Common Sense

When reading an article where some term is used often,... Read More

Five Steps to Rapid Development with TierDeveloper 4.0

Follow the steps below to quickly design, generate, and deploy... Read More

Software Piracy - Global Increase

Pirated software is on the increase and now accounts for... Read More

Lotus Notes Domino and Web: Application Development ? Tips for Programmers

Beginning with Domino version R4 it has integration with the... Read More

Microsoft Office Selecting Secrets

Stop the Runaway MouseWhat's the "runaway mouse?" You've seen it...you... Read More

Snort for Network IDS

What is Snort?Snort is an open source network intrusion detection... Read More

Understanding XML Server

XML Server can be a Web Server that stores the... Read More

What is Groupware?

Vince Lombardi once said that, "The achievements of an organization... Read More

Microsoft Great Plains Integration Manager: Using Continuum ? Overview for Developer

Microsoft Business Solutions Great Plains has I'd say end user... Read More

Outlook... Not Just for Email! Using Your Outlook Calendar

Microsoft Outlook is one of the most widely used software... Read More

Recovering Microsoft Great Plains Customization ? Tips for IT Director

Remember nice and prosperous Clinton era? When you implemented innovative... Read More

Should I Use Windows Update?

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

SSH (SCP) Send Files from Windows to Your Linux Box

Not all of us have the luxury of working both... Read More