Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy aren't there compiler-generated swap() methods in C++0x?
    text
    copied!<p>C++ compilers automatically generate copy constructors and copy-assignment operators. Why not <code>swap</code> too?</p> <p>These days the preferred method for implementing the copy-assignment operator is the copy-and-swap idiom:</p> <pre><code>T&amp; operator=(const T&amp; other) { T copy(other); swap(copy); return *this; } </code></pre> <p>(<a href="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/" rel="noreferrer">ignoring the copy-elision-friendly form that uses pass-by-value</a>).</p> <p>This idiom has the advantage of being transactional in the face of exceptions (assuming that the <code>swap</code> implementation does not throw). In contrast, the default compiler-generated copy-assignment operator recursively does copy-assignment on all base classes and data members, and that doesn't have the same exception-safety guarantees.</p> <p>Meanwhile, implementing <code>swap</code> methods manually is tedious and error-prone:</p> <ol> <li>To ensure that <code>swap</code> does not throw, it must be implemented for all non-POD members in the class and in base classes, in their non-POD members, etc.</li> <li>If a maintainer adds a new data member to a class, the maintainer must remember to modify that class's <code>swap</code> method. Failing to do so can introduce subtle bugs. Also, since <code>swap</code> is an ordinary method, compilers (at least none I know of) don't emit warnings if the <code>swap</code> implementation is incomplete.</li> </ol> <p>Wouldn't it be better if the compiler generated <code>swap</code> methods automatically? Then the implicit copy-assignment implementation could leverage it.</p> <p>The obvious answer probably is: the copy-and-swap idiom didn't exist when C++ was developed, and doing this now might break existing code.</p> <p>Still, maybe people could opt-in to letting the compiler generate <code>swap</code> using the same syntax that C++0x uses for controlling other implicit functions:</p> <pre><code>void swap() = default; </code></pre> <p>and then there could be rules:</p> <ol> <li>If there is a compiler-generated <code>swap</code> method, an implicit copy-assignment operator can be implemented using copy-and-swap.</li> <li>If there is no compiler-generated <code>swap</code> method, an implicit copy-assignment operator would be implemented as before (invoking copy-assigment on all base classes and on all members).</li> </ol> <p>Does anyone know if such (crazy?) things have been suggested to the C++ standards committee, and if so, what opinions committee members had?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload