|
Boost Users : |
Subject: Re: [Boost-users] [Smart Ptr] Why is weak_ptr a _ptr?
From: Marsh Ray (marsh_at_[hidden])
Date: 2010-10-13 14:03:10
On 10/13/2010 04:42 AM, Szymon Gatner wrote:
> Hi,
>
> Is it just me or is it really the worse naming in whole boost library?
I like good naming as much (really more more than) the average
programmer, but sometimes there isn't a great choice. In a case like
this, you're lucky when there's common usage to fall back on.
Finding amusement in the paradoxes that inevitably arise is a good way
to think about and understand them.
> It is nothing like any other smart pointer.
> No initialization / construction from raw pointer.
>
> No operator * or -> (the very least one would expect from _ptr class).
>
> No get().
>
> And many more which I now forgot. My point is that is is not a pointer
> at all, not even a bit.
OK, it's not a pointer. Yet it ends in "_ptr"!
Now, wouldn't it be just great if this were the worst, most
counter-intuitive, inconsistency in the standard language?
However, generations of programmers have grown up with the concepts of
"strong" and "weak" references. In C and C++ these concepts are have
been intimately related to pointers (for obvious reasons).
But it turns out not to be possible in C++ to implement weak references
that look like pointers directly in a way that avoids the possibility of
seriously undefined behavior.
I don't see anything inherent in the concept of a weak reference that
says it has to look syntactically similar to a strong reference. In
fact, it has a more complex interface by definition.
> It only happens to have a member variable returning
> one and that is hardly a reason to call type pointer-like.
No, its interface definition doesn't include any member variables. One
could cook up an implementation which didn't have any either.
I just think of it (loosely) as an object that has a chance (determined
at runtime) of being convertible to a shared_ptr. A lot like
boost::optional really.
> It should rather be called shared_tracker or shared_listener or something
> in that taste ;)
Everybody who's ever used a garbage collected system should know what
"weak" refers to in this context. I don't know what a "tracker" is
supposed to be, and this thing doesn't sound much like a "listener" either.
Play the name game to its logical conclusion and eventually it won't
make a bit of sense to anyone. :-)
- Marsh
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net