|
Boost : |
Subject: Re: [boost] std::map::find() wrapper
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-04 14:24:26
Olaf van der Spek wrote:
> On Wed, May 4, 2011 at 3:08 PM, Stewart, Robert
> <Robert.Stewart_at_[hidden]> wrote:
> > Olaf van der Spek wrote:
>
> >> I'm not sure what your code looks like, but IMO bare
> >> pointers are perfectly fine in certain situations.
> >> Like this one, where no ownership issues are present.
> >
> > A function that returns a pointer immediately begs the
> > question of ownership. It is perfectly reasonable to think
> > that one should call delete on such a pointer when finished
> > with it. That, of course, would have nasty consequences, but
> > it is the result of returning a pointer.
>
> Is it? I've no idea why that would be reasonable. You don't
> delete something just because you can.
It is common for those without sufficient knowledge of smart pointers to return raw pointers from a functions that allocate the memory. Such programmers would suspect the calling code owns the memory referenced by the raw pointer. Thus, such an interface can be a source of errors.
That a person can so readily delete the return value with the attendant ill effects on the container make the interface problematic. An interface that returns a reference doesn't have the same problem. The caller must take the address and then delete it, which requires conscious misuse.
> > Local conventions may dictate that all ownership transfers
> > are done via std::auto_ptr, for example, and never through
> > raw pointers, leaving raw pointers as non-owning, possibly
> > null references. Boost, however, is used in various places
> > with different conventions.
>
> You can't expect every single lib to follow your local
> convention, can you?
No. That was my point. Boost cannot assume local conventions to give proper meaning to a function returning a raw pointer and so should avoid doing so.
_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk