Effective C + + Item 25 Consider writing a swap function that does not throw an exception

Source: Internet
Author: User

This article is Senlie original, reprint please retain this address:Http://blog.csdn.net/zhengsenlie


Experience: When Std::swap is inefficient for your type, provide a Swap member function and determine that the function does not throw an exception

Demo Sample:
The SWAP algorithm in STL
namespace Std{template<typename t>void swap (t &a, t &b) {T temp (a); a = B;b = temp;}} "Pimpl gimmick" (pointer to implementation)--compilation dependencies between files class Widgetimpl{public://...private:int A, B, c;std::vector <double> v;//...} Class Widget{public:widget (const Widget &RHS); Widget &operator= (const widget &RHS) {//Why is the reference returned? --should be a reference, see ITEM10 order operator= return a reference to *this.  //Here is not the same as ITEM28, there is said do not return handler points to the inner component of the object.

... *pimpl = * (Rhs.pimpl);//...} ... Private:widgetimpl *pimpl;}


Resolution: Once you convert two widget object values, the only thing we need to do is convert their pimpl pointers. But the default swap algorithm does not know this. It not only replicates three widgets, but also replicates three Widgetimpl objects.

Correction: Special Std::swap for widgets
Usually we are not agreed to change the Std namespace regardless of the thing. However, it is possible to manufacture a special version number for standard template
Namespace std{//This is the special version number Std::swap for "T is widget".

It's not yet possible to compile Template<>void swap<widget> (widget &a, widget &b) {swap (A.pimpl, B.pimpl);}}


Experience: Suppose you provide a member swap. It is also useful to provide a non-member swap to invoke the former. For classes (not templates). Please also special Std::swap

Demo Sample:
namespace Widgetstuff{class widget{  //can compile normally and be consistent with STL container.

Since all STL containers are also available with the public Swap member function and the Std::swap version number Public://...void swap (Widget &other) {//Call swap should be for std:: Swap uses a using-declaration. The swap is then called and does not take whatever "namespace qualification" using Std::swap; Swap (Pimpl, Other.pimpl);} //...}; void Swap (widget &a, widget &b) {//non-member swap function. This does not belong to the Std namespace A.swap (b);}} Namespace Std{template<>void swap<widget> (widget &a, widget &b) {a.swap (b);}}


Experience: STD templates for "User Defined type" is good. But don't try to add something new to STD within STD.
Demo Sample:
Template<typename t>class widgetimpl{//...} Template<typename t>class widget{//...} Namespace std{//error, illegal---function template can only be fully specific. Cannot be biased template<typename t>void swap<widget<t> > (widget<t> &a, widget<t> &b) { A.swap (b);}} namespace Std{template<typename t>//std::swap an overloaded version number, not legal--STD does not agree to join the new templatesvoid swap (widget<t > &a, widget<t> &b) {A.swap (b);}}

Effective C + + Item 25 Consider writing 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.