Overview of basic concepts of C ++ class template Specialization

Source: Internet
Author: User

In the previous article, we introduced the specific application methods of C ++ class templates in detail. Compared with friends, we should be able to fully understand class templates. Then how does C ++ class template become special? In fact, the special C ++ class template is described as a concept similar to the heavy load.

In my understanding, special processing allows us to perform special processing on some special parameters. The special processing of C ++ class templates is done after the class name. Features are classified into global features and local features. For global specialization, the example in the book is to use deque as the container for the Stack <T> template if the parameter is of the std: string type, while the rest remains unchanged. Therefore, we need to perform the std: string special processing on the Stack <T> template. The Code is as follows:

 
 
  1. #include < deque> 
  2. #include < string> 
  3. #include < stdexcept> 
  4. #include "Stack.h"  
  5. template< > 
  6. class Stack< std::string>{  
  7. private:  
  8. std::deque< std::string> elems;  
  9. public:  
  10. void push(std::string const&);  
  11. void pop();  
  12. std::string top() const;  
  13. bool empty() const{  
  14. return elems.empty();  
  15. }  
  16. };  
  17. void Stack< std::string>::push(std::string const& elem)  
  18. {  
  19. elems.push_back(elem);  
  20. }  
  21. void Stack< std::string>::pop()  
  22. {  
  23. if(elems.empty())  
  24. {  
  25. throw std::out_of_range("Stack< std::string>::pop()==> empty stack.");  
  26. }  
  27. elems.pop_back();  
  28. }  
  29. std::string Stack< std::string>::top() const  
  30. {  
  31. if(elems.empty())  
  32. {  
  33. throw std::out_of_range("Stack< std::string>::pop()==> empty stack.");  
  34. }  
  35. return elems.back();  

Note that the special definition of C ++ class templates is completely different from that of common class templates. The main differences are:

Template <> is added before the special class template, and no parameter is specified. The type parameter is specified after the class name.

In the function definition, all the original type T is changed to the special type std: string. In fact, you can rewrite member functions based on special requirements. You can even define other functions.

Add the source code to the project and compile and run it. You will find that when you use std: string to instantiate the stack, the "overload" version in the StringStack file is actually called. The call of each method is the same. That is to say, special processing is actually required for specific parameters. In this respect, it is indeed similar to the heavy load.

However, I think there is a difference between feature-based and overload-based. Suppose there is a function Func (int, int), and another function loads it as Func (string, string ). In fact, we can also say that the int Func reloads the string Func, Which is mutual. However, this cannot be said by TDE. Because special processing is specific to a certain type of processing, we can say that the special template reloads a template, but it cannot be said that a template reloads the special template. This is a single direction. In addition, if we do not need the Func (int, int) function, we can delete it completely. However, C ++ class templates cannot be exclusive from the dependent class templates. In the preceding example, if you delete the Stack. h file, the definition of the StringStack. h file will fail.

StringStack is a special Stack template. However, the relationship between them is not that close, except for the name. For example, the member functions in the Stack template do not have to appear in StringStack. Similarly, the functions in StringStack do not have to be functions in Stack. That is to say, the special template class can completely rewrite the specified template function as needed, or discard the members in the original template function, and define the member function. There are no restrictions in this regard.

After understanding the global features, it is easy to understand the local features. Local features require that the specified version of the specified class template be used under the specified conditions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.