Item 45: Use a member function template to accept all compatible types

Source: Internet
Author: User

Item 45: Use a member function template to accept all compatible types

Item 45: Use member function templates to accept "all compatible types ".

Item 13 mentioned that the smart pointer can be used to automatically release the memory in the heap. The iterator in STL is also a smart pointer. It even supports the linked list element pointer.++Operation. These advanced features are not available for common pointers. This article uses smart pointers as an example to introduce the use of member function templates:

  • The member function template allows the function to accept all compatible types.
  • If you declare the copy constructor and value assignment operator using the member function template, you still need to manually write the common copy constructor and copy operator. Implicit type conversion

    Although smart pointers provide more useful features than normal pointers, there are also some problems. For example, we have a class level:

    class Top{};class Middle: public Top{};class Bottom: public Middle{};

    A normal pointer can be implicitly converted to a base class pointer by using a derived class pointer:

    Top *p1 = new Bottom;const Top *p2 = p1;

    However, if it is a smart pointer, for example, we have implementedSmartPtr, We need to compile the following code:

    SmartPtr
        
          p1 = SmartPtr
         
          (new Bottom);SmartPtr
          
            p2 = p1;
          
         
        

    Different instances of the same template have no inheritance relationships. In the compiler's opinionAutoPtr AndAutoPtr Are two completely different classes. Therefore, there is a problem with directly compiling the above Code.

    Overload Constructor

    To supportSmartPtr InitializationSmartPtr , We need to reloadSmartPtrConstructor. In principle, we need to write as many reload functions as there are classes. Because the class hierarchy is extended, the number of functions to be overloaded is infinite. Then you can introduce the member function template:

    template
        
         class SmartPtr{public:    template
         
              SmartPtr(const SmartPtr& other);};
         
        

    Note that this constructor is not declaredexplicitTo use the same style as normal pointers. Normal pointers of child classes can be converted to base class pointers by implicit type conversion.

    The constructor that accepts other instances of the same template is called generalized copy constructor ).

    Compatibility type check

    In fact, General constructors provide more functions. He canSmartPtr Implicit conversionSmartPtr , PutSmartPtr ConvertSmartPtr . However, normal pointers do not allow implicit conversions. Therefore, we need to disable them. Note that the implementation of common constructor can solve this problem:

    template
        
         class SmartPtr{public:    template
         
              SmartPtr(const SmartPtr& other): ptr(other.get()){};    T* get() const{ return ptr; }private:    T *ptr;};
         
        

    Inptr(other.get())The compiler checks the compatibility of types only whenUCan be implicitly convertedT,SmartPtrCan be implicitly convertedSmartPtr . This avoids implicit conversion of incompatible pointers.

    Other usage methods

    In addition to implicit type conversion, the member function template also has other purposes, such as the value assignment operator .. Below isshared_ptrPart of the source code:

    template
        
          class shared_ptr{public:    template
         
                  explicit shared_ptr(Y *p);    template
          
                   shared_ptr
           
             const& r>;    template
            
              shared_ptr& operator=(shared_ptr
             
               const& r);};
             
            
           
          
         
        

    We can see the normal pointerY*Toshared_ptr Declaredexplicit, Explicit type conversion is required.shared_ptr Only implicit conversion is required between them. There is a problem when using the copy constructor template: will the compiler generate the default copy constructor? Will a copy constructor be instantiated from your template? That isY == TCompiler behavior in a scenario.

    In fact, the member function template does not change the C ++ rules. C ++ rules: if you do not declare a copy constructor, the compiler should generate one. SoY == TThe copy constructor will not be instantiated from the member function template, but will generate one by itself.

    Soshared_ptrThe template also manually declares the copy constructor:

    template
        
         class shared_ptr{public:    shared_ptr(shared_ptr const& r);    template
         
                  shared_ptr(shared_ptr
          
            const& r);    shared_ptr& operator=(shared_ptr const& r);    template
           
                    shared_ptr& operator=(shared_ptr
            
              const& r);};
            
           
          
         
        

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.