Monday, November 28, 2011

Function Template Specialization

A good study example for function template specialization.


Some notes about function template specialization:
  1. Explicit template specialization should be defined in namespace scope, and hence not inline within class body.
  2. Class templates can be partially specialized, but partial function template specialization is not allowed.
  3. The template parameter list after function name may be omitted, or some trailing arguments omitted. However, it is recommended to keep the <> pair.
  4. Template parameter list is part of signature for function templates and their specializations. Pay attention to the differences between function overloading and partial specialization.
    // Suppose we have the function template
    template< typename T1, typename T2 >
    void func( T1 v1, T2 v2 ) { ... }
    
    // The following is considered as function template overloading and allowed
    template< typename T >
    void func( T v1, T v2 ) { ... }
    
    // The following is considered as partial specialization and not allowed
    template< typename T >
    void func< T, T >( T v1, T v2 ) { ... }
    
Related Posts:
Function Signatures
Class Templates

No comments:

Post a Comment