Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you give me an example when auto_ptr_ref is really necessary?
    primarykey
    data
    text
    <p>I would like to understand better the mechanics and the issues behind creating library and I have decided to start from the std::auto_ptr. I am familiar with the syntax and the usage, however I am trying to understand the implementation details. I have implemented my version of the auto_ptr and it looks like this:</p> <pre><code>#ifndef _AUTO_PTR_H #define _AUTO_PTR_H namespace test { template&lt;typename T&gt; class auto_ptr { public: //ctor, dtor, copy ctor and assignment explicit auto_ptr(T* ptr = 0) throw() : ptr_(ptr) {} auto_ptr(auto_ptr&amp; other) throw() :ptr_(other.release()) {} auto_ptr&lt;T&gt;&amp; operator=(auto_ptr&lt;T&gt;&amp; other) { reset(other.release()); return *this; } template&lt;typename U&gt; auto_ptr&lt;T&gt;&amp; operator=(auto_ptr&lt;U&gt;&amp; other) throw() { reset(other.release()); return *this; } template&lt;typename U&gt; auto_ptr(auto_ptr&lt;U&gt;&amp; other) throw() : ptr_(other.release()) {} //member ~auto_ptr() throw() { delete ptr_; } T&amp; operator*() const throw() { return *ptr_; } T* operator-&gt;() const throw() { return ptr_; } T* get() const throw() { return ptr_; } T* release() throw() { T* temp = ptr_; ptr_ = 0; return temp; } void reset(T* ptr = 0) throw() { if (ptr_ != ptr) { delete ptr_; ptr_ = ptr; } } private: T* ptr_; }; } #endif </code></pre> <p>My class is doing pretty much the job then I have read <a href="https://stackoverflow.com/questions/2121844/what-is-auto-ptr-ref-what-it-achieves-and-how-it-achieves-it">this question</a> and it is clear why auto_ptr_ref is there for. However I would like to have an actual example when the auto_ptr behaves differently without add auto_ptr_ref.</p> <p>My implementation works correctly with this functions:</p> <pre><code>template &lt;typename T&gt; test::auto_ptr&lt;T&gt; source() { return test::auto_ptr&lt;T&gt;(new T()); } template &lt;typename T&gt; void sink(test::auto_ptr&lt;T&gt; bye) { } template &lt;typename T&gt; void useButNotSink(const test::auto_ptr&lt;T&gt; seeYouLater) { std::cout &lt;&lt; *seeYouLater &lt;&lt; std::endl; } </code></pre> <p>With this test program:</p> <pre><code>test::auto_ptr&lt;double&gt; copyConstructed(source&lt;double&gt;()); std::cout &lt;&lt; *copyConstructed &lt;&lt; std::endl; std::cout &lt;&lt; copyConstructed.get() &lt;&lt; std::endl; test::auto_ptr&lt;double&gt; assigned(new double(10)); assigned = source&lt;double&gt;(); std::cout &lt;&lt; *assigned &lt;&lt; std::endl; std::cout &lt;&lt; assigned.get() &lt;&lt; std::endl; *assigned = 2044; std::cout &lt;&lt; *assigned &lt;&lt; std::endl; useButNotSink(assigned); sink(assigned); std::cout &lt;&lt; assigned.get() &lt;&lt; std::endl; </code></pre> <p>I am sure I am missing something,can you give me an example where the auto_ptr_ref struct and the conversion operators are mandatory to get the correct behaviour?</p> <p>Thanks</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.
 

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