Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-03-01 01:47:41


Kevlin Henney wrote:

>>I guess I don't understand the whole concept now. T* -> void* sounds
>>like "narrowing", since it loses information. int -> long never loses
>>information, so "widening" makes sense to me. What is the criterion?
>
> All widening conversions lose type (ie interface and usage) information
> as a trade for widening the range or number of things that they can
> refer to. In the case of int to long, you have lost the information that
> it was originally an int, and could fit into an int. In the case of
> derived * to base * you have lost the information that you were pointing
> to something that was at least a derived. In the case of T * to void *
> you have lost the information that you were dealing with something that
> was at least a T.
>
> In both cases we are dealing with "individuals" and we are widening the
> range or number of individuals we can deal with or refer to. In the
> int-to-long case we are dealing with values, so the notion of set
> containment and its relationship to the widen/narrow metaphor is most
> obvious here. When dealing with pointers we tend to focus on the
> entities that we are referring to, rather than on the pointer value
> itself. A void * can refer to a larger set of objects than a given T *,
> a base * can refer to a larger set of objects than a derived *. However,
> the same conclusion can be reached by considering the pointers as values
> rather than as handles to entities: there are more legal values that a
> void * can take in a program than a T *.
>
> Hope that clarifies the terminology.

Something is still unclear. If the number of individual values a type can
hold is the only criteria for "widening", then is T* -> shared_ptr<T*>
conversion widening? After all, shared_ptr<T*> can hold all pointer values
out there, plus it holds reference count, so the total number of values is
greater.

If that's widening conversion, then constructor of shared_ptr<T*> need not
be explicit, but that would be really unsafe. You say windening conversion
often loose information, and in this fact you can easily loose information
that pointer is allocated on stack, or that it's "this" pointer.

- Volodya


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