Topic: auto_ptr problem?


Author: Chelly Green <chelly@eden.com>
Date: 1996/10/25
Raw View
Donley R. P'Simer wrote:
>
> Donley R. P'Simer wrote:
> > It would be nice if auto_ptr<TYPE> had a conversion to TYPE* but it
> > doesn't. If you don't like the above syntax, try this:
> >         {
> >            Derived* pd = new Derived;
> >            auto_ptr<Derived>(pd);

Declares an object named 'pd' of type auto_ptr<Derived>, but since there is already an
object named 'pd' in scope, it is an error.

> >            ...
> >            foo(pd);
> >            bar(pd);
> >            ...
> >         } // pd gets automagically deleted here!
>
> My apologies. The above code fails to compile on the 2nd line of the
> block. I'm sorry I didn't test it before I posted it. Here's the correct
> code:
>         {
>            ...
>            (auto_ptr<Derived>)(pd);
>            ...
>         }

This will constuct and destruct an auto_ptr<> in the same statement! A simple 'delete pd;'
will do the same thing :-)  In older compilers, the lifetime of the temporary may extend to
the end of the block. Not something to depend on, since it is explicitly specified now to
be destructed at the end of the full statement, which, in this case, is at the end of the
same statement it was constructed in, not at the end of the block!

> For some reason the compiler thought I was redefining sp.

It was a declaration. Strange rule, but things like that are declarations.

--
Chelly Green | chelly@eden.com | C++ - http://www.eden.com/~chelly
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: sean@ocsltd.com (Sean A Corfield)
Date: 1996/10/28
Raw View
In article <3270DF70.1927@eden.com>,
Chelly Green <chelly@eden.com> wrote:

|> > >            Derived* pd = new Derived;
|> > >            auto_ptr<Derived>(pd);
|>
|> Declares an object named 'pd' of type auto_ptr<Derived>, but since there
is
|> already an
|> object named 'pd' in scope, it is an error.

Note that:

      auto_ptr<Derived> pd = new Derived;

fails too. Because this really means:

      auto_ptr<Derived> pd( auto_ptr<Derived>( new Derived ) );

and the 'copy' constructor requires a non-const argument.

|> It was a declaration. Strange rule, but things like that are
declarations.

The disambiguation rule is: if it could be a declaration, it is. More often
than not, it's what is wanted.




[ 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: "Donley R. P'Simer" <donley@mindspring.com>
Date: 1996/10/22
Raw View
Friedrich Knauss wrote:
> ////////////////////////////////////////////////////////
> #include <memory>
> using std::auto_ptr
>
> class Base {...};
> class Derived:public Base {...};
>
> void foo(const auto_ptr<Base> &)
> void bar(const auto_ptr<Derived> &)
>
Why would you want to pass a reference to an auto_ptr into a function
like these two? Just pass the pointer that it holds. e.g.:
 void foo(const Base* p);
 void bar(const Derived* p);

Of course, you would then have to change your calls to these functions
to:
 foo(ap.get());
 bar(ap.get());

It would be nice if auto_ptr<TYPE> had a conversion to TYPE* but it
doesn't. If you don't like the above syntax, try this:
 {
    Derived* pd = new Derived;
    auto_ptr<Derived>(pd);
    ...
    foo(pd);
    bar(pd);
    ...
 } // pd gets automagically deleted here!

> void xxx()
> {
>    auto_ptr<Derived> ap = new Derived;
>
>    foo(ap);    // creation of temporary auto_ptr<Base>??
     ^^^^^^^^
>
>    bar(ap);    // OK, or dangling reference?
> }
> ////////////////////////////////////////////////////////

This fails with the following error (in MSVC++ 4.2):
C:\MSDEV\projects\test\test.cpp(23) : error C2664: 'foo' : cannot
convert parameter 1 from 'class auto_ptr<class Derived>' to 'const
class auto_ptr<class Base> &' (new behavior; please see help)

As far as I can tell from looking at implementations of the standard
C++ library polymorphism is not supported by auto_ptr. In other words,
You cannot initialize auto_ptr<Base> with an auto_ptr<Derived>. You can
initialize auto_ptr<Base> with a Derived*, but I don't see what that
buys you. I may be looking at an out of date implementation (it's the
one included with MSVC++ 4.2), so please correct me if I'm wrong.

auto_ptr is a very minimal implementation. It serves a very specific
purpose and (IMHO) rightly so. Features like 'TYPE& operator*()' and
'TYPE* operator->()' are nice but not necessary to the proper
functioning of the class.

 Donley


[ 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: "Donley R. P'Simer" <donley@mindspring.com>
Date: 1996/10/22
Raw View
Donley R. P'Simer wrote:
> It would be nice if auto_ptr<TYPE> had a conversion to TYPE* but it
> doesn't. If you don't like the above syntax, try this:
>         {
>            Derived* pd = new Derived;
>            auto_ptr<Derived>(pd);
>            ...
>            foo(pd);
>            bar(pd);
>            ...
>         } // pd gets automagically deleted here!

My apologies. The above code fails to compile on the 2nd line of the
block. I'm sorry I didn't test it before I posted it. Here's the correct
code:
 {
    ...
    (auto_ptr<Derived>)(pd);
    ...
 }

For some reason the compiler thought I was redefining sp.

 Donley


[ 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: fknauss@qualcomm.com (Friedrich Knauss)
Date: 1996/10/21
Raw View
I've looked at the DWP, and I couldn't verify or disprove my
assumptions on this problem. I hope someone here can shed some
enlightenment.

It seems that the templated copy constructor for auto_ptr also
serves as a conversion operator. If this conversion is used to
create temporaries, the pointed to object will be deleted,
resulting in dangling references. There is some code below that
tries to illustrate this.

I know auto_ptr<>'s are not a catch all, and that their use
requires some care. I'd like to think, however, that if
polymorphism is supported, that it is supported consistantly.

////////////////////////////////////////////////////////
#include <memory>
using std::auto_ptr

class Base {...};
class Derived:public Base {...};

void foo(const auto_ptr<Base> &)
void bar(const auto_ptr<Derived> &)

void xxx()
{
   auto_ptr<Derived> ap = new Derived;

   foo(ap);    // creation of temporary auto_ptr<Base>??

   bar(ap);    // OK, or dangling reference?
}
////////////////////////////////////////////////////////

--
-- fritzz@qualcomm.com
-- Pain is temporary, glory is forever.
--


[ 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                             ]