<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 16 Function Templates_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 16: Function Templates
 More of this Feature
• Function Templates, Page 1
• Function Templates, Page 2
• Function Templates, Page 3
• Solution to Practice Problem
 
 Related Resources
• C++ Tutorial - Introduction to Pointers
• C++ Tutorial - References
• C++ Tutorial - Arrays and Vectors
• C++ Tutorial - Functions, Basics
• C++ Tutorial - Function Overloading
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• All C++ Lessons
 

by John Kopp

Welcome to About.com's free tutorial on C++ programming. This lesson covers function templates, which are an advanced topic in C++. This lesson only introduces the basics. Templates, which can be used with classes, as well as with functions, are significant because they are used through out the C++ standard library. A major portion of the C++ libraries was once called the Standard Template Library, STL. The most important reason to study templates is to better utilize the C++ libraries. Being able to write your own templates is secondary to this.

Function templates provide a way to parameterize the arguments or return types of a function. What does this mean? In the definition of the function, rather than specifying an explicit type of some of the arguments or the return value, a placeholder is used. Why would this be of benefit? Let's look at the same example that was used in the lesson on function overloading. Suppose it's needed to have a function that converts a temperature from Fahrenheit to Celsius.

Prototype (Declaration):
void ConvertFToC(float f, float &c);

Definition:
void ConvertFToC(float f, float &c)
{
    c = (f - 32.) * 5./9.;
}

Now suppose that a Convert function is also needed to convert an integer Fahrenheit temperature to Celsius in the same program, or a double Fahrenheit to a double Celsius, etc. In the last lesson, two solutions were presented. The first was to create unique functions with unique names for each combination of data types. We ruled out this technique as an effective solution because it could lead to a large number of functions doing essentially the same task with slightly different names. Keeping track of multiple function names is difficult and can lead to programming errors. The second solution was to use function overloading. Function overloading is a technique that allows the programmer to define multiple functions with the same name differing in the number and/or type of their arguments. The compiler chooses the correct function based on the arguments in the function call. This technique worked well. Function definitions were written for each case, and the sample program correctly resolved the right function to call based on the data types in the arguments of the function invocation. The one weakness in this technique was that it was necessary to write multiple function definitions when each definition differed only in the type of its parameters. Function templates provide a way to write a single function definition where the data type is a parameter.

Prototype (Declaration):
template <class Type>
    void ConvertFToC(Type f, Type &c);

Definition:
template <class Type>
    void ConvertFToC(Type f, Type &c)
{
    c = (f - 32.) * 5./9.;
}

As can be seen in this example, a function template begins with the keyword template followed by a bracketed list of parameters. This is required for both declarations and definitions. The rest of the declaration or definition is exactly as it would normally be written, except that in place of an explicit data type, such as int, a token, in this case "Type", is used instead. As also can be seen, although only a single definition is needed, function templates use complicated syntax relative to function overloading.

Next page > Function Templates, Continued. > Page 1, 2, 3


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.