Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    cpp
  » Template Specialization
      by Scott Deming
 Page 1 of 3 
    Next >

(Login to remove green text ads)
Introduction
Introduction to Templates and Template Specialization

Perhaps the biggest buzz about C++ is templates, and generic programming. The use of templates in C++ provides a sort of dynamic typing system while still enforcing the contracts of the underlying data-types. Since templates must resolve completely at compile time there is no risk of runtime type issues inherent in other languages such as Java or C# who only recently introduced templates (aka generics) into their language core.

In C++ templates are resolved at compile-time. This allows the code generator to automatically write the functions or classes based on the types passed in as template parameters. Because the code is built specifically for the type represented, you can count on it being highly optimal with zero overhead.

Template Classes
To create a templated class in C++, you must use the template keyword, followed by a list of parameters enclosed in angle brackets. Each parameter must be a typename, a class, or a constant value.
Code:
template <typename T>
class MyList
{
  /// specification lies here
}
In the above code, we could implement a linked list class wrapping any known type without ever paying much attention to the type itself. This is generic programming, and this is what templates do for us.

Template Functions
We can also use the same process for templatizing functions or methods of a class.
Code:
template<typename L, typename R>
L add(L left, R right)
{
        return left + right;
}
This add() function will take any type for the left and right parameters, add them together, and return the results as the same type provided as the left parameter. Notice previously when I mentioned that templates must be fully resolved at compile time. If you try to invoke this add() function using classes that cannot be added together, then the compiler will reject the code and produce an error. For example:
Code:
cout << add(6, 4.3) << endl;
will work just fine, though the compiler will warn about downgrading a double to an int. However:
Code:
string hello("hello");
cout << add("hello", 4.3) << endl;
will actually produce a compiler error similar to this:
Code:
g++     add.cpp   -o add
add.cpp: In function `L add(L, R) [with L = std::basic_string<char,
   std::char_traits<char>, std::allocator<char> >, R = double]':
add.cpp:25:   instantiated from here
add.cpp:10: no match for `std::basic_string<char, std::char_traits<char>,
   std::allocator<char> >& + double&' operator
make: *** [add] Error 1
Template Specialization
If you were determined to add the string "hello" and double 4.3 together as implied above, you could do so using a C++ template feature called Partial Template Specialization. The following code would make this possible:
Code:
template<typename L, typename R>
L add(L left, R right)
{
        return left + right;
}

template<typename R>
string add(string left, R right)
{
  ostringstream s;
  s << left << right;
  return s.str();
}
Now you can do what was intended previously because the compiler is smart enough to use the template function that was built specifically with the parameter (left) declared as a string. This is partial template specialization because we've only specialized one of the parameters (left), and kept the other as is (right) and dynamic. You will need a modern compiler for this to work. Even though it has been part of the standard for quite some time now, older compilers do not support partial template specialization.




 
 Page 1 of 3 
    Next >

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting