Boost logo

Boost :

From: John Maddock (John_Maddock_at_[hidden])
Date: 2000-08-08 06:05:32


Valentin,

>Nitpicking: is_convertible is meta-function, not a type-trait.
>Type-traits document things. is_convertible examine them.

Nit accepted, however is_convertible is in type_traits.hpp as it seemed to
be the logical place, actually I think it is as much a type-trait as
remove_cv or many of the other traits classes in there - whether one type
is convertible to another is a fundamental property of the two types, yes?
 
>Note also that is_convertible will fail to compile when access
>control is violated.

I don't follow - we're talking about converting one pointer to another -
is_convertible will always compile for pointer conversions or am I missing
something?

>Smart_ptr does not use static_cast.<

It's not used explicitly, the conversions are all implicit:

template<typename T> class shared_ptr {
  public:
// ...
   template<typename Y>
      shared_ptr(const shared_ptr<Y>& r) throw() : px(r.px) {
         ++*(pn = r.pn);
      }
//...
};

The initialisation of member px from r.px involves a cast (from T* to Y*),
it's just not spelt out as such.

>Please explain the semantics of maybe_cast. (You don't have the right
>to use disjonctions (OR) in your definition, of course.)

maybe_cast uses static_cast if there exists an implicit converstion to the
target type, otherwise it uses dynamic_cast. It's purpose is to cast a
pointer to type to a pointer to another type as safely and as efficiently
as possible. For all cases where shared_ptr's template members currently
compile it will use a static_cas; if a static_cast is not possible then it
uses dynamic_cast on the assumption that the type must be polymorphic (of
course if its not a polymorphic type then the code won't compile, but
that's what we would want in that case). Some time ago the inablitiy to
overload dynamic_cast for shared_ptr was discussed; by using maybe_cast
internally the appropriate cast is chosen automatically:

   template<typename Y>
      shared_ptr(const shared_ptr<Y>& r) throw() : px(maybe_cast<Y*>(r.px))
{
         ++*(pn = r.pn);
      }

>BTW, where are do_static_cast, do_dynamic_cast, do_const_cast ?<

I don't follow - maybe_cast is self-contained and is intended as an
implementation detail, there is no need for any other helpers for the
intended purpose.

- John.


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