Chapter Contents |
Previous |
Next |
Templates |
The ANSI/ISO C++ draft form for an explicit specialization is a global declaration of the form:
template < > DECLARATION
The declaration refers to a specialization of a previously declared template or template member. The declaration may be a definition. If the declarator names a function template, an explicit template argument list may optionally follow the template name in order to disambiguate the specialization being declared. For example:
template <class T> class TypeAttr { public: static const int isIntegral; typedef T* Pointer; }; template <class T> const int TypeAttr<T>::isIntegral = 0; // default to // non-integral template < > const int TypeAttr<int>::isIntegral = 1; template < > const int TypeAttr<long>::isIntegral = 1; template <class T> T& deref( typename TypeAttr<T>::Pointer ); // T not // deducible template < > int& deref<int>( int *p ) // have to specify <int> // to disambiguate { return *p }
For compatibility with older compilers, if the
TMPLFUNC
option is not specified,
ordinary function declarations that match the types of template specializations
are treated as explicit specializations. As a special case, for compatibilty
with older compilers, friend declarations inside a template class that use
template parameter names in the declaration type are not treated as specializations.
An example follows:
#include <iostream.h> template <class T> class Vector { // the following uses template // parameter "T", not a specialization friend ostream& operator<< ( ostream&, Vector<T>& ); // the following does not use // template parameters // It is treated as an explicit specialization friend ostream& operator<< ( ostream&, Vector<int>& ); . . . }; // define the template template <class T> ostream& operator<<( ostream& o, Vector<T>& v ) { . . . } // the following definition is also // treated as an explicit specialization ostream& operator<<( ostream& o, Vector<double>& v ) { . . . }
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.