Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Construction, Destruction, and Copying > Whenever it makes sense, provide a no-... - Pg. 100

100 C++ Coding Standards 56. Whenever it makes sense, provide a no-fail swap (and provide it correctly). Summary swap is both a lightweight and a workhorse: Consider providing a swap function to efficiently and infallibly swap the internals of this object with another's. Such a func- tion can be handy for implementing a number of idioms, from smoothly moving ob- jects around to implementing assignment easily to providing a guaranteed commit function that enables strongly error-safe calling code. (See also Item 51.) Discussion A swap function typically looks like this, where U is some user-defined type: class T { // ... public: void swap( T& rhs ) { member1_.swap( rhs.member1_ );