Effective C + + terms of swap

Source: Internet
Author: User

Item 25: A swap function that does not throw an exception

The standard library has a swap for exchanging two object values

Namespace std{

Template<typename t>

void Swap (t& A, t& b)

{

T temp (a);

A = b;

b = temp;

}

}

So, as long as the object type T supports copying (copy ctor and copy assignment), then you can use this standard library swap, but you have to make sure that when you swap, the above operation is what you need, sometimes, not necessary, Especially the Pimpl (pointer to implementation) technique.

For example, the following example:

Class widgetimpl{

Public:

....

Private:

int A, b, C;

....//Many Members

};

Class Widget

{

Public

Widgets (const widgets & RHS);

widget& operator= (const widgets & RHS)

{

*pimpl = *rhs.pimpl//Copy the object referred to

}

Private

Widgetimpl * PIMPL;

};

Once we have two widgets to swap, we can actually have two pointers swapped, and there's no need to swap the pointers to the content, which is too inefficient.

Idea 1:

Namespace std{

Template<>//mission-wide

void Swap<widget> (Widgets & A, widgets & B)

{

Swap (A.pimpl, B.pimpl);

}

}

The above idea compiles, however, because Pimpl is private and cannot be accessed by outsiders.

Idea 2: Join the class's member function swap and let the template function go to invoke

Class widget{

Public:

void Swap (widgets & other)

{

Using Std::swap; Back to talk

Swap (P.impl, Other.pimpl);

}

...

};

Namespace std{

Template<>//mission-wide

void Swap<widget> (Widgets & A, widgets & B)

{

A.swap (b);

}

}

Using the notation above, we can get the correct swap function for the category widget. This is called the Std::swap for your class and makes it call your swap function.

But, the question, if I am now a class template, what do I do?

The class now is:

Template<typename t>

Class widgetimpl{...}

Template<typename t>

Class widget{...}

If you do this now:

Namespace std{

Template<typename t>//partial Special

void Swap<widget> (Widget<t> & A, widget<t> & B)

{

A.swap (b);

}

}

Sorry, the reasons are as follows 2 points:

1. Because C + + only allows the class template to be biased, the function template can not be biased.

2, in the Std namespace, you can not add a new template to the Std. Above, we can add things to Std because, for the standard templates (Eg:swap) to make a special version.

In order to solve the above 22 problems, the following workaround is introduced

Idea 3: Switch to class template

Define a dedicated namespace and put all of our stuff

Namespace widgetstuff{

Template<typename t>

Class widgetimpl{...}

Template<typename t>

Class widget{...}//This has a swap function inside.

Template<tyoename t>

void Swap (widget<t>& A, widget<t>& b)

{

A.swap (b);

}

}

When we use the SWAP function, we should use this:

Template<typename t>

void DoSomething (t& obj1, t& obj2)

{

Using Std::swap; So even without the exclusive version of the Swap function of T

Swap (obj1, obj2);

}

This concludes the process of making our swap function, so why is it possible? This is all based on the C + + name lookup rules.

The compiler uses the "find rule" to find out the swap function in Widgetstuff, if the namespace does not have the exclusive swap function of T, the compiler will use Std::swap, even so, the compiler would like to std::swap the special version of T, that is, the idea 2 will be selected. So, priority order is the idea 3--"idea 2--" standard library own swap

Summarize:

1. Figuring out when we need to write our own swap function

2. Provide a public swap function in class or class template

3. Provide a non-member swap in the namespace of class or class template, calling Swap in 2

4, if you are writing a class (not a template), for the class special Std::swap. And make him call the swap in 2

5, when you actually use swap, first using Std::swap, and then without any modifiers, naked call swap

The member swap must never throw an exception, because many of the exception security codes are guaranteed by the SWAP member function of the class, and we write the swap ourselves, usually with pointers, built-in types that do not throw exceptions, and write swap for efficient

Effective C + + terms of swap

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.