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

GlobeSt.com

New Software Revolutionizes Site Planning
GlobeSt.com
... Areas to create, store and share work orders for properties and facilities. We sat down with Casey Rue, the founder and CEO of Common Areas, to talk about the new software, what users can expect and how it will impact the commercial real estate ...


Xconomy

Xconomy: Quest Analytics Buys Healthcare Compliance Software ...
Xconomy
BetterDoctor, a San Francisco-based startup that has built a national database with information on physicians, dentists, and other practitioners, has been ...

and more »

Forbes

What It Will Take For The Software Services Industry To Navigate A Digital Future
Forbes
No device exists without software, and the end-to-end software services and solutions for businesses across industries are being developed by the information technology (IT) sector. This underlines the need for the sector to be alert to opportunities.


Investor's Business Daily

Software Stocks Flying High, IPOs Drive Up Valuations, M&A May Suffer
Investor's Business Daily
Both Salesforce.com and Workday acquired firms at historically high prices, says Morgan Stanley. The investment bank notes 52 software acquisitions in 2018 thus far. That includes purchases of private firms. Companies bought 36 and 37 software firms, ...


eWeek

Lakeside Software: Product Overview and Insight
eWeek
eWEEK is building a new IT products and services section that encompasses most of the categories that we cover on our site. In it, we will spotlight the leaders in each sector, which include enterprise software, hardware, security, on-premises-based ...


Clarkson professor gets grant to compare DNA software
WatertownDailyTimes.com
Ms. Matthews, an associate computer science professor at Clarkson University, is leading a team that has been awarded a $75,000 Brown Institute Magic grant for her project, Decoding Difference in DNA Forensic Software. ‚ÄúSimply put, when computers are ...


WPRI 12 Eyewitness News

'Significant' UHIP software upgrade planned for this weekend
WPRI 12 Eyewitness News
PROVIDENCE, R.I. (WPRI) - A major software upgrade for Rhode Island's UHIP computer system is scheduled for this weekend. UHIP - short for Unified Health Infrastructure Project - is Rhode Island's problem-plagued computer system, which launched in ...


The Register

From here on, Red Hat's new GPLv2 software projects will have GPLv3 cure for license violators
The Register
Red Hat on Monday said all of its newly initiated open-source projects that adopt GPLv2 or LGPLv2.1 licenses will be expected to include the GPLv3 "cure" provision. The move follows Red Hat's announcement last November, in conjunction with Facebook, ...

and more »

512tech.com

Austin's Malauzai Software acquired by UK-based financial tech company Finastra
512tech.com
Austin-based Malauzai Software has been acquired by London-based financial technology firm Finastra, the companies said Monday. Financial terms of the deal were not released. Founded in 2009 by financial software industry veterans Tom Shen and Robb ...

and more »

TechCrunch

Kaspersky pulls plug on Europol joint venture after EU parliament ...
TechCrunch
Fresh political woes for Russian security firm Kaspersky, which has reacted angrily to a vote in the European Union Parliament last week to ban its software ...

and more »
Google News

Basic Steps To Optimize Your Internet Security

After seeing many people complain about their weak Internet security... Read More

RSS: Get Notified When Your Favorite Websites Are Updated

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

Internet Relay Chat - A Basic Introduction

What is IRC?IRC is Internet Relay Chat. It is a... Read More

Is Your Computer Sick?

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

Theres Gold in Your Websites Server Log

Many Webmasters have never bothered to view their website's server... Read More

ERP Software Financing: the Future? ? Overview for Company Owner

In our case ? we serve Microsoft Business Solutions ERP... Read More

Editing Your Photos Using Microsoft Picture It Publishing Platinum 2002 - A Great Dinosaur

I started using PIP (Picture It Publishing) Platinum 2002 right... Read More

Microsoft CRM Data Conversion FAQ

Microsoft Business Solutions CRM data conversion deserves FAQ type of... Read More

Spyware: What It Is and How to Combat It

Spyware is software or hardware installed on a computer without... Read More

Ukraine IT Myths Dispersed

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

Microsoft Great Plains Upgrade ? Things to Consider and FAQ

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

Vlans

In order to implement VLANs in a network environment, you'll... Read More

How the Firefox: How to... Manual Helped Me

A few months back I really got sick of my... Read More

Software Process Improvement -A Successful Journey

Background: For many organizations like ours, the interim target of... Read More

Device Driver Basics

Most people understand that the "hardware" part of their computer... Read More

Programming Environments And The Software Production Process

Introduction: The creating of a computer program involves a number... Read More

Understanding Document Management

The term "document management" and "paperless office" is the subject... Read More

Microsoft Navision Customization Upgrade ? Tips For Programmer/IT Specialist

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

Chinese Input - Step by Step Instruction on How to Input Chinese Characters in English Windows XP

Enabling Chinese input is quick and easy, there are only... Read More

The XP Firewall Isnt Enough

You might think you don't need a firewall... Read More

Microsoft CRM in Latin America: Implementation, Customization, Support ? Overview for Consultant

Microsoft Business Solutions CRM is present several years on the... Read More

Introducing GRML

Creating a new markup language.Introduction.General Reuse Markup Langauge, or GRML,... Read More

Spyware - The Internet Devil Of Our Times!

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

Microsoft Great Plains international implementation ? USA / Mexico ? overview for consultant

Microsoft Business Solutions Great Plains was purchased from Great Plains... Read More

How to Choose the Right Accounting Software for Your Business

With any good luck and a good amount of hard... Read More