Effective C + + Notes _ clause 25 consider writing out a swap function that does not throw an exception

Source: Internet
Author: User

1 //swap in Lib2 namespaceSTD {3Template<typename t>4     voidSwap (t& A, t&b)5     {6 T Temp (a);7A =b;8b =temp;9     }Ten } One  A //Cons: You need to assign a lot of data, but sometimes you don't have to copy so much content - classWidgetimpl { -  Public: the     //... - Private: -     intA, B, C; -std::vector<Double> v;//There may be a lot of data to replicate for a long time +     //... - }; +  A classWidget { at  Public: -Widgets (Constwidget&RHS); -widget&operator= (Constwidget&RHS) -     { -         //... -*pimpl = *(Rhs.pimpl); in         //.... -     } to Private: +Widgetimpl *Pimpl; - }; the  * //for the above example, only the exchange of pointers is good, there is no need for all the value of the exchange.  $ //in order to solve this problem, the std::swap needs to be specific to the widget. White is specifically for widgetsPanax Notoginseng //design a swap function - //Solution Solutions the classWidget { +  Public: AWidgets (Constwidget&RHS); thewidget&operator= (Constwidget&RHS) +     { -         //... $*pimpl = *(Rhs.pimpl); $         //.... -     } -  the     //attention Yo, the point is up. achieve Real replacement work -     voidSwap (widget&Other )Wuyi     { the         usingStd::swap; - swap (Pimpl, other.pimpl); Wu     } -  About Private: $Widgetimpl *Pimpl; - }; -  - //Note that the following is a special swap for the Widget class that calls the widget's swap function A namespaceSTD { +Template<>//1. Full specificity the     voidSwap<widget> (widget& A, widget& b)//2. Indicates that this swap function is specific to the widget specialization -     { $ A.swap (b); the     } the } the  the //What about the face of class templates? -Template<typename t> in classWidgetimpl { the     //... the }; About  theTemplate<typename t> the classWidget { the     //... + }; -  the /*Bayi error Yo: C + + only allows the class templates to be biased, in function templates is not feasible the customers can fully templates the STD (template<>), but can not add new templates (or classes the or functions or anything else) into Std.  - */ - namespaceSTD { theTemplate<typename t> the     voidswap< widget<t> > (widget<t>& A, widget<t>&b) the     { the A.swap (b);  -     } the }; the  the //-----Solution-----94 namespaceWidgetstuff { the     classWidgetimpl { the      Public: the         //...98     Private: About         intA, B, C; -std::vector<Double> v;//There may be a lot of data to replicate for a long time101         //...102     };103 104     classWidget { the      Public:106Widgets (Constwidget&RHS);107widget&operator= (Constwidget&RHS)108         {109             //... the*pimpl = *(Rhs.pimpl);111             //.... the         }113  the         //attention Yo, the point is up. achieve Real replacement work the         voidSwap (widget&Other ) the         {117             usingStd::swap;118 swap (Pimpl, other.pimpl);119         } - 121     Private:122Widgetimpl *Pimpl;123     };124  theTemplate<typename t>126     voidSwap (widget<t>& A, widget<t>&b)127     { -A.swap (b);//calling the swap function in the widget129     } the}

Summarize:

When Std::swap is inefficient for your type, provide a swap member function and make sure that the function does not throw an exception.

If you provide a member swap, you should also provide a non-member swap to invoke the former. For classes (not templates), please also special std::swap.

You should use the using declaration for Std::swap when you call swap, and then call swap without any namespace qualification adornments.

It is good to have a "user defined type" for the Std template, but never try to add something new to Std.

Effective C + + Notes _ clause 25 consider writing out a swap function that does not throw an exception

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.