Boost logo

Boost :

From: Kevin Atkinson (kevinatk_at_[hidden])
Date: 2000-03-19 05:01:39


On Sun, 19 Mar 2000, Nathan Myers wrote:

> On Sat, Mar 18, 2000 at 10:01:31AM -0500, Mark Borgerding wrote:
> > Greg Colvin wrote:
> > > (somebody) wrote:
> > > > The idea was that the implicit casts like:
> > > > operator T*() const throw() { return ptr; }
> > > > be replaced with:
> > > > operator void*() const throw() { return ptr; }
> > > > [so that] such a pointer can be checked for NULLness...
> > > > but cannot be dereferenced by accident.
> > >
> > > The impasse is that some users of smart pointers, myself
> > > included, actually want operator T*(). If you have generic
> > > code written to accept pointers then operator void*() won't
> > > do, and neither will get().
> > ...
> > I think the slight unwieldiness of using get() is a small
> > price to pay for fewer runtime errors. If I have to choose between
> > compile-time checks and ease of use, I will choose the compile-time
> > checks nearly every time.
>
> Indeed, some of us argued for naming them member of auto_ptr<> that
> reveals the raw pointer value "auto_ptr<>::leak()".

With an auto pointer you almost have to use get because if you use another
auto pointer you transfer ownership. If you need to say pass the pointer
to a function you need to use get(), what the hell is wrong with that.

> To me it seems
> a grave, grave design error to provide an automatic conversion for
> that purpose.

And the purpose will be.....

> Automatic conversions are dangerous enough in general,
> but when they contradict the base purpose of the component, it makes
> me wonder why somebody wants such a component at all.

Could you elaborate.

> Very frequently people ask why basic_string<> has no automatic conversion
> to char const* (or, worse, char*). The answer is the same.

No, the reason is with basic string the temporary may go away sooner than
you think.

I have not used smart_pointer but if I do use it I will only use it when I
NEED the reference counting. I want it to be treated like a pointer for
all other purposes. I won't pass the pointer to another function unless I
know I won't delete it or take ownership of it in any way.

There is not very much danger in the auto conversion except in an example
given earlier when a function returns a smart_pointer.

Thus I think it should be made an option in some way. And you should not
think less of people who use the option.

---
Kevin Atkinson
kevinatk_at_[hidden]
http://metalab.unc.edu/kevina/

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk