Boost logo

Boost :

From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2007-05-25 15:59:06


>> I'd like to request the following functions added to shared_ptr's
>> interface:
>>
>> template <class T>
>> void * shared_ptr<T>::void_cast() const;
>
> I'd rather spell that 'pointer()' or something along those lines for
> consistency with 'type()'.

I like pointer() better, yes.

>> template <class T>
>> std::type_info const & shared_ptr<T>::type() const;
> <snip>
> But consider the following slightly more interesting example:
>
> struct Base
> {
> virtual ~Base() {}
> };
>
> struct Derived: Base
> {
> int i;
> };
>
> Derived * pd = new Derived;
> Base * pb = pd;
> shared_ptr<void> pv( pb );
>
> Now there are two reasonable interpretations; either
>
> pv->pointer() == pb && pv->type() == typeid(Base);
>
> or
>
> pv->pointer() == pd && pv->type() == typeid(Derived);
>
> Which one should shared_ptr pick?

I had in mind the second interpretation from your example, that is, ::type()
would return the "object type" as defined in [1.8.1].

There is also the case when a shared_ptr is initialized with a pointer to
incomplete (or void) type and a custom deleter, when the "object type" is
unavailable for shared_ptr to pick up automatically. Therefore, if ::type()
and ::pointer() are supported, it also makes sense to provide constructor(s)
that take the std::type_info const & to be returned by ::type(). This way
someone could presumably achieve the first interpretation from your example:

Derived * pd = new Derived;
Base * pb = pd;
shared_ptr<void> pv( pb, typeid(Base) );

Emil Dotchevski


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