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

Oswego taxi to Midway ..
In The News:

Hackers and scammers keep looking for ways to break into your Facebook account. Kurt “CyberGuy" Knutsson has some tips to make sure your account is secure.
Kurt “CyberGuy" Knutsson shows you how to create group texts to reach multiple people at once — and also how to leave them if you get annoyed at the volume of interruptions.
Tax fraud is on the rise, and hackers are now using identity hijacking and artificial intelligence for tax returns in someone else's name to get a refund.
Toyota's Punyo soft robot mimics human behavior, using its chest, hips and arms — in addition to its hands — like a human to handle objects,
Hackers used third-party information to break into Roku accounts, according to the streaming company. Roku says it has secured affected accounts.
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 explains the simple steps for setting a Wi-Fi hot spot on an Android. If you have internet on your phone, it's easy.
Dubai recently hosted the first jet suit race with a backdrop of skyscrapers, and one observer says it was straight out of a comic book.
Kim Komando introduces ways to keep you and your devices safe while traveling. The clever hacks mentioned can protect you from hackers and jackers.
TikTok CEO Shou Chew published a video Wednesday urging users to "protect your constitutional rights" against a bipartisan bill that would force China to divest from the app.
Tech guru Kurt "CyberGuy" Knutsson says you can never be too careful with tech passwords. He explains how to keep your information safe.
The Lighthouse in New Zealand rotates 360 degrees, constantly giving those inside different views around the clock. Built in the 1990s, it's up for sale.
High school students in Colorado are learning how to design their own AI model projects, including cameras for self-driving cars and apps that can teach other languages.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
Vehicles collect sensitive personal information, which is used to set auto insurance rates. That's partly why auto insurance premiums are rising.
Honda's Uni-One is a hands-free electric mobility device that can reach speeds of 3.7 mph and support a user weighing up to 242 pounds.
Kim Komando puts three AI chatbots head-to-head to decide which is the best assistant for modern problems. Compare Google Gemini, ChatGPT, and Perplexity.
To safeguard personal information on iPhones, users can employ built-in security measures and iCloud Keychain for enhanced privacy. Kurt “CyberGuy" Knutsson shows you how.
Kurt “CyberGuy" Knutsson introduces you to the City Transformer’s CT-2 electric car, which can seamlessly transition from 8 feet wide on the road to a compact 3 feet for parking.
Hackers are always looking for new ways to trick targets into downloading malware. Now, they're after cryptocurrency folks via Calendly. Kurt “CyberGuy" Knutsson has the details.
Kurt “CyberGuy" Knutsson reveals how the Microlino Lite, a compact and eco-friendly vehicle, tackles city traffic congestion with its chic design and small dimensions.
A new funeral scam targets surviving family members of those who recently died. Scammers reach out to the survivors and pose as funeral home officials.
Merlin Labs' autonomous pilot could soon hit the commercial airways and may help alleviate a looming pilot shortage, according to CEO Matt George.
There's a simple way to check your subscriptions on an Android and cancel them. Tech guru Kurt "CyberGuy" Knutsson explains how it is done.
Hackers use pirated software to target macOS, Android and Windows devices in search of stealing cryptocurrency, according to Kurt "CyberGuy" Knutsson.

Programming Language Migration Path

While I was preparing some personal background information for a... Read More

CRM 101: Customer Relationship Management for Beginners

Customer Relationship Management, abbreviated "CRM," is the term for a... Read More

Review on QuarkXpress 6.0

After almost two decades of existence, Quark has become the... Read More

Behave, Word, Behave!

If you copy something from a Web site or elsewhere...... Read More

Microsoft CRM Data Conversion FAQ

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

Microsoft CRM Conversation Gateway: VoIP - Implementation & Customization

Microsoft CRM is winning market share step-by-step from such the... Read More

Microsoft CRM ? Consulting in the Post-Recession Time

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

Software Companies: Generate New Revenue Streams and Decrease Costs with Custom e-Learning Content

It's no secret that software companies operate in a very... Read More

Should I Use Windows Update?

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

25 Things Mapping Software Can Do For You

1. With mapping software you can create a report that... Read More

Great Plains Dynamics on Pervasive/Ctree support ? overview for consultant

All of us know that Microsoft bought former Great Plains... Read More

Five Tips For A Great Software Demo

Whether you need to close a sale, gather end-user feedback,... Read More

Cisco Certification: Introduction To ISDN, Part III

Configuring PPP PAP AuthenticationNow we know how the ISDN link... Read More

Microsoft CRM USA Nationwide Remote Support

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

Microsoft Navision Customization and Reporting ? Tips For Programmer/IT Specialist

C/SIDE (Client/Server Integrated Development Environment) - The core of... Read More

How To Make Good Use of Spreadsheets

Most computer users use spreadsheets software such as Microsoft Excel... Read More

Windows Screensavers Explained

In this article you will find some background information about... Read More

Microsoft Great Plains Nationwide Remote Support

ERP Consulting industry is on the way to serve clients... 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

ERP Implementation: Success Factors

As seeing large number of implementations ? in our case... Read More

What is Shareware?

Shareware is software that you can try before you buy;... Read More

Cisco Certification: Introduction To ISDN, Part V

The major reason I recommend getting your hands on real... Read More

Microsoft CRM: Data Conversion ? Import from Act!

Best Software Act! is very popular CRM for small and... Read More

Databases ? How We Love to Hate Them!

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

15 Questions to Ask Your Software Vendor

When making a decision to buy any piece of software... Read More

shuttle from Midway Morris ..