Topic: Bool conversion for auto_ptr


Author: bvisscher@spyder.net (Bruce Visscher)
Date: 1996/10/21
Raw View
On 15 Oct 1996 23:48:58 GMT, sean@delta.com (Sean L. Palmer) wrote:
>I wrote:
>> OTOH, after using the auto_ptr class a few times, I have been
>> wondering why there's no bool operator!().  Has this been considered?

>Then again since there is no implicit conversion to T*, this would be
>desirable to automatically convert to *something*, namely bool, so that we
>didn't have to use get() all the time to see if it's valid.

>There is also in the new auto_ptr flavors an owned() or owner() call (I
>forget) to detect whether the auto_ptr owns the object it points to.

Are you sure?  It isn't in the version that was posted to this group
by Greg Colvin, the auto_ptr's author.  Also, it is missing from the
DEC C++ implementation which is supposed to be based on the latest
draft (post January 1996 DWP which I don't have access to).

>This
>is a similar thing to what we'd want operator bool() to do, but operator
>bool() should just tell us whether the pointer is not null.

I agree.  That is what I had in mind for operator !().  I should have
said so in my original post.  IMHO, if you are worried about who owns
the auto_ptr then you are probably using the wrong class.  A reference
counted pointer class would be better in that case.

>Because the committee hasn't provided explicit conversion operators yet, an
>ambiguity would be introduced if operator bool() const and operator T*
>const() were both added.  They may be trying to reserve the right to later
>add an implicit conversion to T* for some reason.

I take it you think that the proposal for an explicit conversion
operator (being discussed in another thread) will pass.  I agree that
if it does that an "explicit operator bool() const" might be in order
for auto_ptr.  I don't think that I'd favor a cast to T* or const T*
though.  I think that breaks encapsulation too much.

Another thing I've been wondering about auto_ptr is: shouldn't there
be a template argument for allocator?  As it stands, the auto_ptr
class seems rather out of place without one.  Now that you're
recoiling in horror with "But that would add even more overhead to the
class!", I would add that I think it should be specialized for the
standard allocator (which should of course be the default) to have
exactly the same semantics and foot-print that it does now (modulo the
explicit type cast and operator!() being discussed here).

Bruce
---
[ 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@delta.com (Sean L. Palmer)
Date: 1996/10/15
Raw View

> OTOH, after using the auto_ptr class a few times, I have been
> wondering why there's no bool operator!().  Has this been considered?

Then again since there is no implicit conversion to T*, this would be
desirable to automatically convert to *something*, namely bool, so that we
didn't have to use get() all the time to see if it's valid.

There is also in the new auto_ptr flavors an owned() or owner() call (I
forget) to detect whether the auto_ptr owns the object it points to.  This
is a similar thing to what we'd want operator bool() to do, but operator
bool() should just tell us whether the pointer is not null.

Because the committee hasn't provided explicit conversion operators yet, an
ambiguity would be introduced if operator bool() const and operator T*
const() were both added.  They may be trying to reserve the right to later
add an implicit conversion to T* for some reason.




[ 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/16
Raw View
In article <01bbbaf0$41e0f5c0$0a2920cc@landspeeder.delta.com>,
Sean L. Palmer <sean@delta.com> wrote:
>There is also in the new auto_ptr flavors an owned() or owner() call (I
>forget) to detect whether the auto_ptr owns the object it points to.  This
>is a similar thing to what we'd want operator bool() to do, but operator
>bool() should just tell us whether the pointer is not null.

Does anyone have a description of the changes to auto_ptr since the
April WP? All I've got is the implementation for the changed auto_ptr
from Scott Meyer's AWL page, and it has no mention of any type of
ownership detecting member...


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