Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is rather confusing. Basically, <code>auto_ptr_ref</code> exists because the <code>auto_ptr</code> copy constructor isn't really a copy constructor in the standard sense of the word.</p> <p>Copy constructors typically have a signature that looks like this:</p> <pre><code>X(const X &amp;b); </code></pre> <p>The <code>auto_ptr</code> copy constructor has a signature that looks like this:</p> <pre><code>X(X &amp;b) </code></pre> <p>This is because <code>auto_ptr</code> needs to modify the object being copied from in order to set its pointer to 0 to facilitate the ownership semantics of <code>auto_ptr</code>.</p> <p>Sometimes, temporaries cannot match a copy constructor that doesn't declare its argument <code>const</code>. This is where <code>auto_ptr_ref</code> comes in. The compiler won't be able to call the non-const version of the copy constructor, but it can call the conversion operator. The conversion operator creates an <code>auto_ptr_ref</code> object that's just sort of a temporary holder for the pointer. The <code>auto_ptr</code> constructor or <code>operator =</code> is called with the <code>auto_ptr_ref</code> argument.</p> <p>If you notice, the conversion operator in <code>auto_ptr</code> that automatically converts to an <code>auto_ptr_ref</code> does a <code>release</code> on the source <code>auto_ptr</code>, just like the copy constructor does.</p> <p>It's kind of a weird little dance that happens behind the scenes because <code>auto_ptr</code> modifies the thing being copied from.</p> <h2>Random related tanget about C++0x and unique_ptr</h2> <p>In C++0x, <code>auto_ptr</code> is deprecated in favor of <code>unique_ptr</code>. <code>unique_ptr</code> doesn't even <strong>have</strong> a copy constructor and uses the new 'move constructor' which is explicit about the fact that it will modify the object being moved from and leave it in a useless (but still valid) state. Temporaries (aka rvalues) are explicitly always allowed to be arguments to a move constructor.</p> <p>The move constructor in C++0x has a number of other big benefits. It enables the standard STL containers to store <code>unique_ptr</code>s and do the right thing, as opposed to how <code>auto_ptr</code>s cannot be. It also mostly eliminates the need for the 'swap' function as the whole purpose of the swap function is usually to be a move constructor or move assignment operator that never throws.</p> <p>Which is the other expectation. The move constructor and move assignment operator (much like a destructor) are never supposed to throw.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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