Boost logo

Boost :

From: Giovanni P. Deretta (gpderetta_at_[hidden])
Date: 2006-02-06 18:43:09


Peter Dimov wrote:
> [...]
> shared_ptr<socket> is the easy part, although this immediately leads me to
> ask the obvious question: why isn't a socket already a
> shared_ptr<socket_impl>?
>

While Christopher can certainly answer better than i can, i think that
the answer is: because it often doesn't need to be! A socket is a non
copyable value based object. It doesn't need dynamic allocation and
could very well be stack based (as the datagram_socket and the
socket_acceptor in the example).

Only if you need the lifetime of the object to be dynamic (as the
example does, but it is *not* always the case) you directly allocate it.
Or make it be a member of a class that is itself dynamically allocated.

You could make the socket class hold internally a shared_ptr to a
socket_impl and make it (shallow) copyable, but it would it be
surprising and would require dynamic allocation(*) even if not needed.

A nice solution would be to make socket movable...

(*) Not really, all copies of stream could hold a copy of socket_imp
and only the last one would close it, but it would still require some
sort of reference counting.

-- 
Giovanni P. Deretta

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