Topic: Is dynamic_cast safe?


Author: lars.farm@nts.mh.se (Lars Farm)
Date: 1996/02/27
Raw View
     5.2.6  Dynamic cast                           [expr.dynamic.cast]
   ...
   6 Otherwise,  v shall be a pointer to or an lvalue of a polymorphic type
   ...
What should happen when 'v' is not a pointer to or an lvalue of a
polymorphic type? return 0? crash? throw bad_cast? undefined?
   ...
   8 The  run-time  check  logically  executes  like  this:
     [...]  Otherwise, the run-time  check fails.

If fail means undefined, why not require that it returns 0?

--
Lars Farm, lars.farm@nts.mh.se


[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/02/27
Raw View
In article AD58B8389668E1A9@sleipner.nts.mh.se, lars.farm@nts.mh.se (Lars Farm) writes:
>     5.2.6  Dynamic cast                           [expr.dynamic.cast]
>   ...
>   6 Otherwise,  v shall be a pointer to or an lvalue of a polymorphic type
>   ...
>What should happen when 'v' is not a pointer to or an lvalue of a
>polymorphic type? return 0? crash? throw bad_cast? undefined?
>   ...
>   8 The  run-time  check  logically  executes  like  this:
>     [...]  Otherwise, the run-time  check fails.

>If fail means undefined, why not require that it returns 0?

The dynamic_cast has two failure modes, depending on whether the cast is
of pointer or reference type (the only two kinds allowed). The last
paragraph of the section says a failed pointer cast returns the null
pointer, and a failed reference cast throws the bad_cast exception.
The behavior is completely defined
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: tony@online.tmx.com.au (Tony Cook)
Date: 1996/02/28
Raw View
Lars Farm (lars.farm@nts.mh.se) wrote:
:      5.2.6  Dynamic cast                           [expr.dynamic.cast]
:    ...
:    6 Otherwise,  v shall be a pointer to or an lvalue of a polymorphic type
:    ...
: What should happen when 'v' is not a pointer to or an lvalue of a
: polymorphic type? return 0? crash? throw bad_cast? undefined?

No, the compiler should produce a diagnostic, see [intro.compliance]
paragraphs 1 through 3.

:    ...
:    8 The  run-time  check  logically  executes  like  this:
:      [...]  Otherwise, the run-time  check fails.

: If fail means undefined, why not require that it returns 0?

This one is made pretty clear in the following paragraph:

9 The value of a failed cast to pointer type is the null pointer value
  of the required result type.  A failed cast to reference type throws
  bad_cast (_lib.bad.cast_).

--
        Tony Cook - tony@online.tmx.com.au
                    100237.3425@compuserve.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]