Topic: Dereferencing Unowned auto_ptr
Author: danm@ziplink.REMOVE_THIS.net (Dan Muller)
Date: 1997/08/02 Raw View
In article <0033000003708203000004*@MHS>, WSEYMOUR@email.usps.gov says...
> I naively asked:
> >
> > Can anyone think of a scenario in which it's safe
> > to dereference an auto_ptr that doesn't own its
> > pointee? [snip]
> >
> and I haven't yet seen a response in this newsgroup.
>
> Although absence of evidence isn't evidence of absence,
> my guess (supported, I think, by other auto_ptr-related
> postings by Kanze et al.) is that one ought never to
> dereference an unowned auto_ptr because, in general,
> there's no way to know whether the pointee has been
> deleted.
>
[sip]
>
> Is there a rationale for the other view?
Nothing so grand as a rationale, but how about an example of how I've
found this behavior useful?
I've had situations in which object A needs the services of an object B.
B may either be supplied from outside of A (typically in the
constructor), or may be created by A. In the former case, A does not own
B and should not delete it. In the latter case, A owns B and B should be
deleted when A is.
I have found the auto_ptr handy in this case. When a pointer to
B is supplied by an outside agent, I initialize the auto_ptr and
immediately release it, but continue to use it. Certainly there are other
ways of coding this, and it may not match some people's idea of the
purpose of an auto_ptr, but it seemed a natural solution at the time.
--
Dan Muller danm@ziplink.net
http://www.ziplink.net/~danm
---
[ 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: WSEYMOUR@email.usps.gov
Date: 1997/07/28 Raw View
I naively asked:
>
> Can anyone think of a scenario in which it's safe
> to dereference an auto_ptr that doesn't own its
> pointee? [snip]
>
and I haven't yet seen a response in this newsgroup.
Although absence of evidence isn't evidence of absence,
my guess (supported, I think, by other auto_ptr-related
postings by Kanze et al.) is that one ought never to
dereference an unowned auto_ptr because, in general,
there's no way to know whether the pointee has been
deleted.
Therefore, in keeping with the principle that one
shouldn't have to pay for what one doesn't use,
the auto_ptr implementation that nulls its private
pointer is preferable to having a bool hanging around
simply to allow dereferencing an auto_ptr that one
probably shouldn't be dereferencing anyway.
Is there a rationale for the other view?
--Bill
---
[ 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: WSEYMOUR@email.usps.gov
Date: 1997/07/19 Raw View
Can anyone think of a scenario in which it's safe
to dereference an auto_ptr that doesn't own its
pointee? (I suppose that if I have multiple copies
of an auto_ptr all in the same (identical) scope,
I can dereference any of them equally safely;
but I can't imagine why I'd need to do that.)
--Bill
---
[ 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 ]