Boost logo

Boost :

Subject: Re: [boost] std::map::find() wrapper
From: Olaf van der Spek (ml_at_[hidden])
Date: 2011-05-04 16:32:03


On Wed, May 4, 2011 at 10:14 PM, Stewart, Robert <Robert.Stewart_at_[hidden]> wrote:
> void
> h()
> {
>   foo * p = f();
>   delete p; // not unreasonable
>   foo & r = g();
>   delete &r; // misuse
> }
>
> g()'s interface returns a reference and not a pointer.  That is a clear indication that g()'s caller does not own the memory.  By contrast, f() returns a pointer.  The caller is not unreasonable in thinking ownership might be transferred, particularly if local conventions indicate as much.
>
> An interface that precludes the possibility for that sort of confusion is better.

You're ignoring the fact that such an interface makes normale usage
more complex.
Also, the difference between delete p and delete &r is very small, if
the user does not understand how ownership works he might just write
delete &r too.
I don't get why you think it's reasonable to assume ownership is
transfered by a find function.

Olaf


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