7.2 Instantiation and Specialization

Ru-Brd

The process of creating a regular class, function, or member function from a template by substituting actual values for its arguments is called template instantiation . This resulting entity (class, function, or member function) is generically called a specialization .

However, in C++ the instantiation process is not the only way to produce a specialization. Alternative mechanisms allow the programmer to specify explicitly a declaration that is tied to a special substitution of template parameters. As we introduced in Section 3.3 on page 27, such a specialization is introduced by template<> :

 template <typename T1, typename T2>  // primary class template  class MyClass {   };  template<>  // explicit specialization  class MyClass<std::string,float> {   }; 

Strictly speaking, this is called a so-called explicit specialization (as opposed to an instantiated or generated specialization ).

As introduced in Section 3.4 on page 29, specializations that still have template parameters are called partial specializations :

 template <typename T>  // partial specialization  class MyClass<T,T> {   };  template <typename T>  // partial specialization  class MyClass<bool,T> {   }; 

When talking about (explicit or partial) specializations, the general template is also called the primary template .

Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net