Topic: dynamic_cast<T> implementation...


Author: rmartin@rcmcon.com (Robert Martin)
Date: 1995/10/26
Raw View
Michael Cook <mcook@cognex.com> writes:

>What if I use `dynamic_cast' where `static_cast' would be sufficient?  Is the
>type /still/ required to be polymorphic?  For example, given this:

> struct B {};
> struct D : B {};
> D* d;

>is this expression valid?

> dynamic_cast<B*>(d)

As I understand the DWP, the argument of dynamic_cast need only be
polymorphic in those cases where there is no implicit conversion from
the argument to the requested type.
--
Robert Martin       | Design Consulting   | Training courses offered:
Object Mentor Assoc.| rmartin@oma.com     |   OOA/D, C++, Advanced OO
2080 Cranbrook Rd.  | Tel: (708) 918-1004 |   Mgt. Overview of OOT
Green Oaks IL 60048 | Fax: (708) 918-1023 | Development Contracts.


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1995/10/20
Raw View
Michael McCarty (mikem@eai.com) wrote:
|> I confess I haven't read much of the draft standard and frankly I hope I never
|> have to, but I do have one question that is puzzling me:

|> How exactly is dynamic_cast<T> supposed to be implemented in the compiler?

|> The implementation of this worries me a bit.  I can't really imagine this
|> functionality having a practical implementation (i.e. without breaking a LOT of

|> code).

|> It seems to me that the only way to implement this is to attach a type
|> 'pointer' to every object instantiated, similar to virtual function pointer
|> tables.  If this is so, I strongly disagree with this implementation and, if it

|> came to pass in the standard, would strongly consider migrating back to C
|> because of it.

Dynamic_cast only works on objects with at least one virtual function.
In this case, there is already a `type pointer' attached, at least in
the common implementation.  Or what else would you call the pointer to
the virtual function tables.
--
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
                             --Beratung in industrieller Datenverarbeitung

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: martelli@cadlab.cadlab.it (Alex Martelli)
Date: 1995/10/21
Raw View
"Michael McCarty" <mikem@eai.com> writes:
 ...
>How exactly is dynamic_cast<T> supposed to be implemented in the compiler?

The standard, of course, does not specify that.

>It seems to me that the only way to implement this is to attach a type
>'pointer' to every object instantiated, similar to virtual function pointer

In "dynamic_cast<T> v", except for quite a few cases which can be solved
at compile time (e.g., where type T is a pointer to a base class of what
v points to), v "shall be a pointer to or an lvalue of a polymorphic type"
(cfr expr.dynamic.cast sub 6).  Therefore a "type 'pointer'" or equivalent
functionality will only be needed for types that have at least one vptr
anyway, and indeed one could imagine minimizing the overhead by enriching
the vtable structure -- e.g. just adding to each vtable of a derived
class a pointer to the vtable of the parent class (assuming a multiply
derived class has separate vtables for its various derivations); in
which case, on "dynamic_cast<D*> bp" (with bp declared as a B* and
D deriving from B), the compiler might run-time check by following
the chain of such pointers upwards from *bp's (along the appropriate
inheritance path towards B) and see if it encounters the vtable of
class D (relevant to its descending from B).  I'm sure far more clever
implementations exist (ones that pay a constant time for the check
rather than one proportional to the number of levels of derivation),
but this at least shows that the space overheads need not be much --
just one extra pointer per vtable of each polymorphic class...


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: Michael Cook <mcook@cognex.com>
Date: 1995/10/22
Raw View
>>>>> "JM" == Jason Merrill <jason@cygnus.com> writes:

>>>>> Michael McCarty <mikem@eai.com> writes:
 >> How exactly is dynamic_cast<T> supposed to be implemented in the compiler?

 >> It seems to me that the only way to implement this is to attach a type
 >> 'pointer' to every object instantiated, similar to virtual function
 >> pointer tables.

 >> Tell me it ain't so...

 JM> It ain't so.  Dynamic typing is only supported for polymorphic types --
 JM> classes with virtual functions.  The RTTI pointer goes into the vtable in
 JM> all implementations I know about.

What if I use `dynamic_cast' where `static_cast' would be sufficient?  Is the
type /still/ required to be polymorphic?  For example, given this:

 struct B {};
 struct D : B {};
 D* d;

is this expression valid?

 dynamic_cast<B*>(d)

(Why would I write such code?  Well, the types `B' and `D' might have been
parameters to a template in which I did not want to assume the use of
`static_cast' would be proper.)

Michael.


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: martelli@cadlab.cadlab.it (Alex Martelli)
Date: 1995/10/23
Raw View
Michael Cook <mcook@cognex.com> writes:
 ...
>What if I use `dynamic_cast' where `static_cast' would be sufficient?  Is the

Yes, I think this holds in general.

>type /still/ required to be polymorphic?  For example, given this:

> struct B {};
> struct D : B {};
> D* d;

>is this expression valid?

> dynamic_cast<B*>(d)

This surely is.  Draft standard 5.2.6 explains the semantic of
dynamic_cast<T>(v), and sub 5 it gives the possibility...:

5. If T is "pointer to cv1 B" and v has type "pointer to cv2 D" such that
   B is a base class of D, the result is a pointer to the unique  B  sub-
   object of the D object pointed to by v.



Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]