Topic: auto_ptr (operator=)


Author: Irfan Pyarali <irfan@cs.wustl.edu>
Date: 1997/09/08
Raw View
Here is the interface for the standard C++ auto_ptr:

 namespace std {
    template<class X> class auto_ptr {
    public:
      typedef X element_type;
    // _lib.auto.ptr.cons_ construct/copy/destroy:
      explicit auto_ptr(X* p =0) throw();
      auto_ptr(const auto_ptr&) throw();
      template<class Y> auto_ptr(const auto_ptr<Y>&) throw();
      auto_ptr& operator=(const auto_ptr&) throw();
      template<class Y> auto_ptr& operator=(const auto_ptr<Y>&) throw();

     ~auto_ptr();
    // _lib.auto.ptr.members_ members:
      X& operator*() const throw();
      X* operator->() const throw();
      X* get() const throw();
      X* release() const throw();
    };
  }

An operation like the one below is not allowed:

class foo {};

auto_ptr <foo> auto_foo;
...
auto_foo = new foo; // error

instead one has to do:

auto_foo = auto_ptr (new foo);

This sucks because (1) the constructor is explicit and (2) there is no
operator=(const X*)

Anybody knows why this is broken?

Irfan
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Mark Rodgers" <mark.rodgers@xtra.co.nz>
Date: 1997/09/10
Raw View

Irfan Pyarali <irfan@cs.wustl.edu> wrote in article
<3411DF52.DC1B50C0@cs.wustl.edu>...
> Here is the interface for the standard C++ auto_ptr:
<snip>
> An operation like the one below is not allowed:
>
> class foo {};
>
> auto_ptr <foo> auto_foo;
> ...
> auto_foo = new foo; // error
>
> instead one has to do:
>
> auto_foo = auto_ptr (new foo);
>
> This sucks because (1) the constructor is explicit and (2) there is no
> operator=(const X*)

Actually it is worse because you missed the <foo> from the assignment.  It
should have been

 auto_foo = auto_ptr<foo>(new foo);

I don't think I have ever seen an uglier line of code in all my life so I
agree that the auto_ptr is broken.   However, I think the constructor
should be explicit and I think an operator=(X*) would be a mistake.

The April 95 DWP contained a reset function so you would write:

 auto_foo.reset(new foo);

This seemed to do the job perfectly well but got dropped for some reason.
I too would like to know why the auto_ptr is now so broken and unless it
gets fixed, I'll continue using my own version:

namespace CadenzaNewZealandLtd
{
template <class T> class auto_ptr {
  public:
    explicit auto_ptr(T *q = 0) : p(q) {}
    auto_ptr(const auto_ptr<T> &q) : p(q.p) { q.p = 0; }
    auto_ptr &operator=(const auto_ptr<T> &q)
    {
        if (this != &q) { delete p; p = q.p; q.p = 0; }
        return *this;
    }
    ~auto_ptr() { delete p; }
    T *get() const { return p; }
    T &operator*() const { return *p; }
    T *operator->() const { return p; }
    T *release() { T *q = p; p = 0; return q; }
    T *reset(T *q) { T *r = p; p = q; return r; }
  private:
    mutable T *p;
};
}
--
Mark
mark.rodgers@xtra.co.nz
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]