Effective C + + clause 45 using member function templates to accept all compatible types

Source: Internet
Author: User

1. "Smart pointers" are objects that behave like pointers, but they can provide functionality that pointers do not: shared_ptr,weak_ptr,auto_ptr (see clause 13) implements automatic management of heap memory, and STL iterators implement the traversal of the entire container.

The advantage of a true pointer is that it supports the conversion of a derived class pointer to a base class pointer in an inheritance hierarchy (of course, the standard library shared_ptr,weak_ptr,auto_ptr is implemented).

2. Because there is no direct link between the different bodies of the same template, that is, for a custom smart pointer (assuming named SMARTPTR), if no additional means are taken to support the conversion of a derived class pointer to a base class pointer in the base level, then smartptr<base> And smartptr<derived> will be considered irrelevant by the compiler, and there is no implicit conversion of smartptr<derived> to smartptr<base>.

To obtain the implicit conversion capability between smart classes, explicitly write the constructor for implicit conversions.

Assuming that there is a Base class, to implement the conversion of the derived class > to smartptr<base> by Smartptr<base, it is not clear that all the copy constructors for Smartptr are written for the implicit type conversion. Because the expansion possibilities of base's inheritance system are infinite. Therefore, only member function templates can be used once and for all:

Template<typename t>class  smartptr{public:    template<typename U >    smartptr (const smartptr<u>& other);  // generate a copy constructor for implicit conversions     ....}
View Code

The meaning of the copy constructor of the above code smartptr is that for any type U and type T, a smart<t> can be generated based on the type smart<u>, because smartptr<u> and smartptr<t > is a different body of the same template, so this constructor is called the generalization copy constructor. The generalization copy constructor for Smartptr is not declared as explict to mimic the implicit conversion between the original pointers.

    The declaration of the copy constructor above does not restrict conversions between smartptr to the original convertible pointers, so the conversion between smartptr in the SMARTPTR implementation is limited, assuming smartptr like auto_ PTR provides a get function to get the raw pointer, the implementation of SMARTPTR (const smartptr<u>& Other) might look like this:

Template<typename t>class  smartptr{public:    template<typename U >    smartptr (const smartptr<u>& Other): Helder (other.  Get()) {}    Tgetconst  {        return  haldptr;    }    ... Private :    T* heldptr;}
View Code

This puts the task of checking if the underlying pointer can be converted to the underlying pointer from the row check.

3. The utility of member template functions (Member templates function) is not limited to constructors, another common function of which is to support assignment operations. TR1 's shared_ptr supports all "from compatible" built-in pointers, Tr1::shared_ptrs, The tectonic behavior of Auto_ptrs and Weak_ptrs ", and all assignment operations from the above (except weak_ptr), are excerpted from an excerpt from the TR1 specification for TR1::SHARED_PTR:

Template<typename t>classshared_ptr{ Public: Template<classY>//class and TypeName mean the same when declaring type parametersExplict shared_ptr (y*p); Template<classY>shared_ptr (shared_ptr<Y>Const&R); Template<classY>explict shared_ptr (weak_ptr<Y>Const&R); Template<classY>explict shared_ptr (auto_ptr<Y>Const&R); Template<classY>shared_ptr&operator= (shared_ptr<y>Const&R); Template<classY>shared_ptr&operator= (auto_ptr<y>Const&R); ...}

All of the above constructors are explict except for the generalization copy constructor, which means that shared_ptr allows for implicit type conversions between shared_ptr, and prohibits the original pointer or other smart pointer types from swapping to shared_ptr's live slaves; The auto_ptr passed to the Tr1::shared_ptr constructor and the assignment operator is not declared const, because AUTO_PTR is then set to null after this.

4. It is important to note that although the member function template (member) can be used to present a copy constructor for shared_ptr<t> to shared_ptr<t> conversion, However, if you do not declare a copy constructor, you will not be able to rely on the representation of the member function template, and you should manually declare a normal copy constructor. The copy assignment operator is the same.

Effective C + + clause 45 using member function templates to accept all compatible types

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.