Can you give an example when auto_ptr_ref is really needed?

I would like to better understand the mechanics and problems of creating a library, and I decided to start with std :: auto_ptr. I am familiar with the syntax and usage, however I am trying to understand the implementation details. I have implemented my version of auto_ptr and it looks like this:

#ifndef _AUTO_PTR_H
#define _AUTO_PTR_H

namespace test
{    
    template<typename T>
    class auto_ptr
    {
        public:
            //ctor, dtor, copy ctor and assignment
            explicit auto_ptr(T* ptr = 0) throw() 
                : ptr_(ptr) {}

            auto_ptr(auto_ptr& other) throw()
                :ptr_(other.release()) {}

            auto_ptr<T>& operator=(auto_ptr<T>& other)
            {
                reset(other.release());
                return *this;
            }

            template<typename U>
            auto_ptr<T>& operator=(auto_ptr<U>& other) throw()
            {
                reset(other.release());
                return *this;
            }

            template<typename U>
            auto_ptr(auto_ptr<U>& other) throw() 
                : ptr_(other.release()) {}

            //member
            ~auto_ptr() throw() { delete ptr_; }

            T& operator*() const throw() { return *ptr_; }

            T* operator->() 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

My class does most of the work, after which I read this question , and it is clear why auto_ptr_ref is needed. However, I would like to have an actual example where auto_ptr behaves differently without adding auto_ptr_ref.

My implementation works correctly with these functions:

template <typename T>
test::auto_ptr<T> source()
{
    return test::auto_ptr<T>(new T());
}

template <typename T>
void sink(test::auto_ptr<T> bye)
{
}

template <typename T>
void useButNotSink(const test::auto_ptr<T> seeYouLater)
{
    std::cout << *seeYouLater << std::endl;
}

With this test program:

test::auto_ptr<double> copyConstructed(source<double>());

std::cout << *copyConstructed << std::endl;
std::cout << copyConstructed.get() << std::endl;

test::auto_ptr<double> assigned(new double(10));
assigned = source<double>();

std::cout << *assigned << std::endl;
std::cout << assigned.get() << std::endl;
*assigned = 2044;

std::cout << *assigned << std::endl;
useButNotSink(assigned);
sink(assigned);
std::cout << assigned.get() << std::endl;

, - , , auto_ptr_ref ?

+3
2

test::auto_ptr<double> copyConstructed(source<double>());

. MSVC , ++. , auto_ptr_ref.

auto_ptr_ref MSVC 6.0, std::auto_ptr, , . ​​ , - .
+4

, . const reference. const,

test::auto_ptr<double> copyConstructed(source<double>());

( source<double>() ).

( , ++ 0X rvalue ).

+4

All Articles