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/.

Batchtown Chicago prom limo .. Lockport Chicago limo O’Hare
In The News:

A virtual private network can help ensure your information remains security and your privacy remains intact. Kurt the CyberGuy explains.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
Artificial intelligence-based cameras are giving air defense operators unprecedented capabilities in monitoring and protecting airspace.
Apple's iOS 18.1 Inactivity Reboot automatically reboots your iPhone if it hasn't been used or unlocked for more than three days, providing better data protection.
An inventor designed rooftop solar panels for a Tesla that draws solar energy while the car is parked, adding travel mileage without plugging in.
There are currently no laws governing what artificial intelligence can and cannot do with the information it gathers; here are 10 things to avoid telling AI chatbots to keep yourself safe.
A credit union with over 240,000 members recently revealed it was targeted by cybercriminals, resulting in a data breach that was part of a two-month attack by hackers.
Scammers have become skilled at creating convincing fake websites that can easily fool unsuspecting users. The CyberGuy offers tips to protect yourself.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
CAPTCHAs, which are used by websites to confirm whether users are people or bots, are harmless, but hackers are using them to infect PCs with malware.
Hackers recently leaked personal information of about 500,000 Americans and stole patient medical records that included lab results and insurance details.
The holiday season sees a rise in mobile shopping scams. Tech expert Kurt “CyberGuy" Knutsson helps you learn how to stay safe.
Tech expert Kurt “CyberGuy" Knutsson says a VPN enhances online banking security by encrypting data and protecting privacy.
Beware of these six sneaky holiday scams. Tech expert Kurt “CyberGuy" Knutsson gives you tips to avoid falling victim.
Tech expert Kurt “CyberGuy" Knutsson reveals how to securely back up and factory reset your Android to protect your privacy and data.
Artificial intelligence is making life easier for cybercriminals, allowing them to create elaborate scams to trick people. Kurt the Cyberguy explains how to protect yourself.
Cut through all the digital clutter and delete multiple emails from your Android simultaneously. Kurt the CyberGuy explains how it's done.
Tips to prevent your holiday decorations from being stolen
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
Kurt "CyberGuy" Knutsson lays out the immediate steps you should take if your phone has been hacked and your personal information becomes vulnerable.
Fraudsters are sending people bogus invoices through PayPal as part of a sneaky scam that is going around; here's how to protect yourself from being fooled.
A former Colgate-Palmolive employee was shocked to discover $750,000 had been drained from her 401(k) account. "CyberGuy" offers tips on how to prevent identity theft.
Electric vehicle maker Harbinger recently showed its electric delivery truck can handle icy roads with agility and stability in winter.
To make the busiest time of year more manageable, here are some tricks for tracking your packages, taking quality family photos and curating the perfect Christmas playlist.
Kurt "CyberGuy" Knutsson explains how to keep your online Amazon gift purchases a secret from loved ones or friends this holiday season.

10 Things You Could be Using Photoshop For, But Probably Arent

Most people don't use Photoshop to its fullest capabilities. Here... Read More

Five Steps to Rapid Development with TierDeveloper 4.0

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

Anti-Spyware Protection ? Holes in the Shining Armor

Looking at all the ads which promise to get rid... Read More

Databases ? How We Love to Hate Them!

You've finally created databases that you can actually use to... Read More

Microsoft Great Plains POP: Purchase Order Processing ? Overview For Consultants

Great Plains Purchase Order Processing (POP) module makes up one-third... Read More

Snort for Network IDS

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

Change Your Word Docs in Record Time!

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

Great Plains Dexterity: Customizations & Source Code Programming

Great Plains Software Dynamics, Dynamics C/S+, eEnterprise were written on... Read More

Software Automation Helps Increase your Bottom Line

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

Microsoft Great Plains Upgrade ? Version 8.0 Overview for IT Director/Controller

If you have Microsoft Great Plains as main accounting and... Read More

Microsoft Great Plains customization ? Freight Forwarding/Transportation industry example

Microsoft Business Solutions Great Plains version 8.5, 8.0, 7.5, Great... Read More

Adware and Spyware

What is 'adware'?Adware is basically software or scripts that are... Read More

C++ Function Templates

C++ Function templates are those functions which can handle different... Read More

Professional Software Icons For Your Standalone Application

User interfaces and accessibility are some of the most important... Read More

Microsoft CRM Development

Microsoft CRM is CRM answer from Microsoft Business Solutions.The whole... Read More

Microsoft CRM USA Nationwide Remote Support

Remember old good days when your company probably had Great... Read More

Microsoft Great Plains version 8.5: Upgrade, Customization, VBA, Crystal Reports - Highlights

Microsoft Great Plains is one of the Microsoft Business Solutions... Read More

Monitoring Software Can be Used for Spying as Well

We all already got used to computer monitoring both at... Read More

Great Plains Customization Upgrade? Overview For CIO/IT Director

Around the same time Microsoft made its move with .Net... Read More

Microsoft Update - 5 Reasons to Update Service Pack 2

Microsoft released Service Pack 2 (SP2) for the Windows XP... Read More

Microsoft Great Plains Data Conversion ? Overview For Developer

Looks like Microsoft Great Plains becomes more and more popular,... Read More

Corporate ERP Selection: Microsoft Great Plains

In our opinion, traditional approach when you select ERP/MRP system... Read More

The Bluebird Project

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

Microsoft CRM Integration with Lotus Notes Domino: Messaging Connector ? Future Directions

IBM Lotus Notes Domino and Microsoft CRM (Client Relation Management)... 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

shuttle from Midway Munster are ..