Home arrow C++ arrow Page 4 - Templates in C++

Templates in C++

Templates have been helping programmers to change the way they are able to code since the ealy 1990s. Since that time they have advanced rapidly, and in this tutorial we will take a close look at them and learn to use them to effectively decrease our tasks.

Author Info:
By: Gabor Bernat
Rating: 3 stars3 stars3 stars3 stars3 stars / 9
June 17, 2008
  1. · Templates in C++
  2. · Starting with Templates
  3. · Class Templates
  4. · Template Specialization

print this article

Templates in C++ - Template Specialization
(Page 4 of 4 )

Furthermore, you can specify an individual implementation for a class for which you want a specific solution. Maybe you are creating a class that, even though it is general, it has an exception -- a situation that you need to treat separately. This can be resolved through template specialization. And the method used to complete this is the following specialization-let's go back to the function template because it's more didactic and shorter:


int function<char>( char first, char second)


return first > second ? first : second;



Using the written method is nothing devilish, just as usual you would use:

char a = 'A';

char b = 'B';

char result;

result =static_cast<char> ( function(a, b));

cout << result << endl;

result =static_cast<char> ( function(b, a));

cout << result << endl;

The upper code snippet will print the character that is higher by ASCII code to the console (cout). So this will be the 'B' character twice. If you paid attention, by now you should also realize that the static_cast function is a template, and one that is called to the type you specify between the <>. As mentioned previously, this can be done for classes too and the method is the same.

There is one more thing we should mention. You may want to add non-type parameter(s) to your template. This consists of adding a type as a parameter in the template. Back to the initial problem:

template< int Number ,typename Type,class T >

int function( Type first, T second)



return memcmp(&first, &second,sizeof(first)*Number);


And using it shouldn't be a problem to understand:

char a = 'A';

char b = 'B';

int result;

result = function< 0>(a, b);

cout << result << endl;

result = function<1, char, char>(a, b);

cout << result << endl;

Basically, you give a constant to the function at function call. The number variable is a constant within the function (or the class if you write it for it) and can't be changed in the code snippet inside the function. This makes it expedient to use this method each time you need to initialize a value in a template.

For example, if you want to create a class that has Number length strings/arrays within it, you can do this by passing the size of the string at creation. Of course, in various other places, just use your imagination. For class templates, you may also add default values for non-type parameters. You are only forced to give the number parameter because the rest can be deduced by the compiler. However, if you write it explicitly in the code, the compiler won't mind at all.


We've arrived at the end and by now you should know everything to be known about template class creation. You should become a genuine expert with just a little practice. Please don't forget that programming is just like love, you can't learn it from books. In order to understand it, you must make a move. Start up your compiler and write a couple of lines. See the problems, try them and don't forget: don't hesitate because affirmation without act means nothing.

Now may be the perfect time to get a good introduction into the libraries I mentioned at the beginning. The STL is a perfect place to start. Check out the "Introduction to STL," which will be published on Dev Articles.

See you next time, and if you have any questions regarding the things we learned so far, feel free to ask in the forums. This is a place for a community where evolution is present at every second, and you can join in. Have a nice weekend and keep on coding!

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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