Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::auto_ptr Compile Issue in Visual Studio 6.0
    primarykey
    data
    text
    <p>Update: Edited code example to use AutoA for the workaround (which was the original intention). Realized this after seeing rlbond's answer.</p> <p>I am trying to incorporate the usage of <code>auto_ptr</code> in my code based on recommendations from this thread:</p> <p><a href="https://stackoverflow.com/questions/478482/express-the-usage-of-c-arguments-through-method-interfaces/478678">Express the usage of C++ arguments through method interfaces</a></p> <p>However, I am receiving some unexpected compile errors when compiling with Visual Studio 6.0. It has a problem when dealing with assignments/copies of a <code>std::auto_ptr</code> of a derived type to a <code>std::auto_ptr</code> of the base type. Is this an issue specific to my compiler?</p> <p>I know there's a strong recommendation to use Boost, but on my project it is not an option. If I still want to use <code>auto_ptr</code>, am I forced to use the workaround of calling <code>std::auto_ptr::release()</code>? From what I have encountered so far, this issue results in a compiler error, so it's easy enough to catch. However, could adopting the convention of calling release to assign to a 'auto_ptr' of base type throughout expose me to any maintenance issues? Especially if built with a different compiler (assuming other compilers don't have this issue).</p> <p>If the <code>release()</code> workaround is not good due to my circumstances, should I fall back on using a different convention for describing transfer of ownership?</p> <p>The following is an example illustrating the problem.</p> <pre><code>#include "stdafx.h" #include &lt;memory&gt; struct A { int x; }; struct B : public A { int y; }; typedef std::auto_ptr&lt;A&gt; AutoA; typedef std::auto_ptr&lt;B&gt; AutoB; void sink(AutoA a) { //Some Code.... } int main(int argc, char* argv[]) { //Raws to auto ptr AutoA a_raw_to_a_auto(new A()); AutoB b_raw_to_b_auto(new B()); AutoA b_raw_to_a_auto(new B()); //autos to same type autos AutoA a_auto_to_a_auto(a_raw_to_a_auto); AutoB b_auto_to_b_auto(b_raw_to_b_auto); //raw derive to auto base AutoB b_auto(new B()); //auto derive to auto base AutoA b_auto_to_a_auto(b_auto); //fails to compile //workaround to avoid compile error. AutoB b_workaround(new B()); AutoA b_auto_to_a_auto_workaround(b_workaround.release()); sink(a_raw_to_a_auto); sink(b_raw_to_b_auto); //fails to compile return 0; } </code></pre> <p>Compile Error:</p> <pre><code>Compiling... Sandbox.cpp C:\Program Files\Microsoft Visual Studio\MyProjects\Sandbox\Sandbox.cpp(40) : error C2664: '__thiscall std::auto_ptr&lt;struct A&gt;::std::auto_ptr&lt;struct A&gt;(struct A *)' : cannot convert parameter 1 from 'class std::auto_ptr&lt;struct B&gt;' to 'struct A *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called C:\Program Files\Microsoft Visual Studio\MyProjects\Sandbox\Sandbox.cpp(47) : error C2664: 'sink' : cannot convert parameter 1 from 'class std::auto_ptr&lt;struct B&gt;' to 'class std::auto_ptr&lt;struct A&gt;' No constructor could take the source type, or constructor overload resolution was ambiguous Error executing cl.exe. Sandbox.exe - 2 error(s), 0 warning(s) </code></pre>
    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.
 

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