|
Boost : |
From: Alex Burton (alexibu_at_[hidden])
Date: 2007-06-19 18:36:01
> Alex Burton wrote:
>
>> Marcus wrote:
>>
>>>> It allows my code to be free of the * pointer notation.
>>>> All pointers look the same, ie shared_ptr<Foo>, scoped_ptr<Foo>,
>>>> weak_ptr<Foo>
>>>>
>>>>
>>>>
>>> Why is that desirable?
>>>
>>>
>> It is desirable so that that the pointers look the same, this means
>> that fewer mistakes when changing between pointer types as code
>> changes.
>> It is also desirable (at least for me) so that in my code I
>> explicitly see the intent of the pointer.
>> When (now simple_ptr) is used the programmers intent is to only look
>> at the object pointed to, not to own it or share it.
>> When a normal pointer is used, the intent is unclear.
>>
>>
> IMHO raw pointers are excellent for showing that you're only
> looking. If
> you intend to handle ownership, you'd better wrap it in something
> smarter as fast as possible. :)
>
Yes, thats exactly how I was using raw pointers (and keeping my code
free of the delete keyword).
However when the code changes from Foo * to scoped_ptr<Foo> or from
auto_ptr<Foo> to Foo *, it seems much more logical and consistent to
change from simple_ptr<Foo> to scoped_ptr<Foo> or from auto_ptr<Foo>
to simple_ptr<Foo>.
It also requires slightly less key presses in my opinion, as only the
name of the pointer type has to be changed, where as with raw
pointers it requires edits on both sides of the type name.
I suppose my point is now that C++ developers have been so clever to
have written smart pointers, and that those smart pointers require a
different form to the raw pointers, then to keep the language looking
consistent it makes sense to get rid of the raw pointers entirely and
leave them to become another relic from C that C++ programmers have
access to but choose not to use.
> Therefore I am a bit confused, since, most of the time, a
> smart_pointer
> class implies some form of memory management. But that's not always
> the
> case. I've seen smart-pointers used to select thread-specific data
> (not
> just boost::thread_specific_pointer, which also handles ownership)
> etc.
Yes it is unprecedented as in usually a smart pointer does something
smart.
If smart pointers are pointers that do something smart then it is not
one.
If smart pointers are template classes with overloaded -> and *
operaters then it is one.
>
>>>> vector<Foo> foos;
>>>>
>>>> BOOST_FOREACH(weak_ptr<Foo> f,foos)
>>>> {
>>>> //do something with f->
>>>> }
>>>>
>>>>
>>> OTOH, the for_each could use an indirect_iterator-range instead, to
>>> iterate over IFoo& instead.
>>>
>>>
>> Yes but that would require a change to the code.
>>
>>
> True. It all depends where you want to put the deref-or-not logic.
> (either in the pointer type or in the container or in a fancy
> make_range-function.)
The container example is just one, there are many instances where
taking a simple_ptr from a Foo object, which may change to a
scoped_ptr<Foo> object, etc make this useful.
Alex
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk