Home arrow C++ arrow Page 2 - The Mighty C++ Template

The Mighty C++ Template

One of the biggest advantages of using C++ is templates. Templates were designed from the ground up to allow developers to write one function to handle many different types of parameters. In this article, Mitchell will explain what both function and class templates are, and give examples of each.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 48
November 23, 2001
  1. · The Mighty C++ Template
  2. · Understanding function overloading
  3. · The template
  4. · The template (contd.)
  5. · A simple class template
  6. · A simple class template (contd.)
  7. · Instantiating the class
  8. · Behind the scenes
  9. · Conclusion

print this article

The Mighty C++ Template - Understanding function overloading
(Page 2 of 9 )

To understand where templates have come from though, you must first understand function overloading. Let's say for an example, that we wanted to create a function that would work with numeric values and return the average of those values. The function should be able to accept integers, floats and doubles. To accomplish this, we could create three functions to handle each different type of numeric value:

To work with integers:

int GetAverage(int num1, int num2, int num3)


return static_cast<int>((num1 + num2 + num3) / 3);


To work with floating point values:

float GetAverage(float num1, float num2, float num3)


return static_cast<float>((num1 + num2 + num3) / 3);


To work with double values:

double GetAverage(double num1, double num2, double num3)


return static_cast<double>((num1 + num2 + num3) / 3);


Then, whenever we called the GetAverage() function from our program, the C++ compiler would decide which function it should call based on the types of the parameters passed to that function. If an un-handled type, such as long was passed to the function, then the compiler would automatically cast it to the type that it most closely resembled (In this case, long would be converted to int).

As you can see, we end up with three functions that do exactly the same thing. This is bad, because it creates a lot of redundant code, and our executable file will be bigger than it needs to be.

There has to be a better way... and there is... it's called a template.
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials