shared_ptr type_info, void cast

I'd like to request the following functions added to shared_ptr's interface: template <class T> void * shared_ptr<T>::void_cast() const; template <class T> std::type_info const & shared_ptr<T>::type() const; The first function mimics the use of dynamic_cast<void *>(p) for pointers to polymorphic objects. The second function is similar to typeid(r), for references to polymorphic objects. Of course, void_cast needs to be specialized to apply the correct modifiers of T to the returned pointer type. There are two scenarios that need those functions: - when using shared_ptr<T> in contexts where T is incomplete (or void); - when the T in shared_ptr<T> has nothing to do with the type of the object managed by the shared_ptr's control block. For example, through the recently introduced aliasing constructor, it is possible for a shared_ptr<int> to manage the lifetime of an object of some user-defined type that has the int as a member. Both functions are critical for implementing proper serialization of shared_ptr objects. Emil Dotchevski

Emil Dotchevski wrote:
I'd rather spell that 'pointer()' or something along those lines for consistency with 'type()'.
template <class T> std::type_info const & shared_ptr<T>::type() const;
There's an interesting ambiguity here. The easy case is X * px = new X; shared_ptr<void> pv( px ); where pv->pointer() == px && pv->type() == typeid(X). 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 like pointer() better, yes.
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

On 5/23/07, Peter Dimov <pdimov@mmltd.net> wrote:
[snip]
[snip]
or
pv->pointer() == pd && pv->type() == typeid(Derived);
Sorry if it is too obvious. But how can shared_ptr implement pv->pointer() == pd ? It takes pb from the constructor, it doesnt know anything about Derived.
[snip]
Thanks, -- Felipe Magno de Almeida
participants (3)
-
Emil Dotchevski
-
Felipe Magno de Almeida
-
Peter Dimov