homesubmit linklatest hitstop hitscontact usprivacy policyabout us

Global Index Directory » Computers & Internet » Templates

Our Sponsors:
Our Partners:
Templates Article
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.

Muthukumar


More articles can be found at http://www.codersource.net/.

Links Sort by: PageRank | Hits | Alphabetical

Free Wordpress ThemesFree Wordpress Themes - http://www.skinpress.com

Download high quality widget ready wordpress themes to use on your blogs for free. Free support available. - more

Templates Web – Web Templates, Graphics, Scripts and Web ToolsTemplates Web – Web Templates, Graphics, Scripts and Web Tools - http://www.templates-web.com

Website Templates Package with over 2000 FrontPage, Dreamweaver, flash, HTML, Adobe, Photoshop and auction web templates. One stop shop for all you web development and promotion tools. We also provide other web utilities like scripts and applets, search engine marketing tools and web graphics and accessories. - more

Powerpoint Templates - TemplatesWise.comPowerpoint Templates - TemplatesWise.com - http://www.templateswise.com

Great collection of backgrounds, templates and more inspirational graphics to build stunning presentations, all templates are free to download. - more

Myspace AddonsMyspace Addons - http://www.marcsfreetools.com/

Offers MySpace stuff including backgrounds, icons, graphics, cursors, codes, tweaks, layout generators and other addons. - more

Myspace BackgroundsMyspace Backgrounds - http://www.killacodes.com

Find the best MySpace Layouts, MySpace Graphics, MySpace Tweaks, MySpace Images, MySpace Generators and Myspace Backgrounds on Killacodes.com. Large Selection Of Myspace Backgrounds, look for yourself. - more

Website templates by The Template ShackWebsite templates by The Template Shack - http://www.thetemplateshack.com

Offering website templates, complete Flash sites, Flash intros, WordPress themes, and thousands of other website types and styles. Complete web site packages ready to be adapted to fit your web development requirements. - more

Free templates, Logo, BottonFree templates, Logo, Botton - http://www.webaccessories.org

Free templates, buttons, logo, icon, background. Pick from our selection of free hand-made professional web accessories. You can download the free PSD files and customized it. We provide cool free web page templates which are very professional and can be used free for personal and business website use. We add new free website templates frequently. - more

CSS templates & DJ flash web page templatesCSS templates & DJ flash web page templates - http://www.webpagetemplate.ws/

Web page templates suitable for business and personal use. Flash animation intros and web sites. Full website templates, just replace and complete. Cromatic and a-cromatic artwork for your online face. - more

Website TemplatesWebsite Templates - http://www.yahootemplates.com

Yahootemplates is the leading provider of professionally design web templates, flash templates and many other website templates. Only professional designers were used to create these worldclass templates that are 100% coded properly and accurately. We also offer custom-coded templates at great prices. - more

Car Web TemplateCar Web Template - http://www.autodealersdesign.com

We providing complete website design solution for auto dealers by ready made websites templates and layouts which will help car dealer and auto dealer to make their websites. - more

Auto Dealer Website TemplatesAuto Dealer Website Templates - http://www.cardealerstemplates.com

We are offering auto dealer web site templates and car dealership templates for car dealership website design. Make your auto dealer website with our automotive dealership website designer tool. - more

Auto Dealership Web Site TemplatesAuto Dealership Web Site Templates - http://www.autodealerstemplates.com

We are providing automotive website template and car website templates to auto dealer website and also car dealership websites. Our automotive dealership website designer software makes these things easy to build your auto dealer websites. - more

Car Website TemplatesCar Website Templates - http://www.cardealersdesign.com

Car dealers design is providing you auto dealers website templates for auto dealer to built their website more easily. This allows you to make your automotive dealer websites and car dealership websites. - more

zorg phplinkdirectory templateszorg phplinkdirectory templates - http://www.zorg-directory.com/phpld-themes/

Zorg directory has several free phpLD themes for you to use on your phplinkdirectory powered web directory. There are several themes to use, all with different layouts and colors. The themes come with a readme file to help with installation. Every theme is coded for all 5 versions of phpLD. - more

Myspace LayoutsMyspace Layouts - http://www.copymyspace.com

Large selection of MySpace layouts and codes, customize your myspace profile in seconds. - more

Flash TemplatesFlash Templates - http://flashytemplates.blogspot.com/

We list the latest designs from young and talented flash artists from around the globe. Download high quality flash templates in a matter of minutes! They even come with well documented code. Be smart, Save money and time today. Prices from $1 and up. - more