[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gcc
Subject:    dynamic_cast (bug 3(?))
From:       Carlo Wood <carlo () runaway ! xs4all ! nl>
Date:       1999-11-15 15:12:47
[Download RAW message or body]

Hiya,

the standard says (ok, I only got the latest draft :/):

  5.2.7  Dynamic cast                                [expr.dynamic.cast]

  1 The  result of the expression dynamic_cast<T>(v) is the result of con-
    verting the expression v to type T.  T shall be a pointer or reference
    to a complete class type, or "pointer to cv void".  Types shall not be
    defined in a dynamic_cast.  The dynamic_cast operator shall  not  cast
    away constness (_expr.const.cast_).

  2 If  T is a pointer type, v shall be an rvalue of a pointer to complete
    class type, and the result is an rvalue of type T.  If T is  a  refer-
    ence  type,  v  shall  be  an lvalue of a complete class type, and the
    result is an lvalue of the type referred to by T.

I think that you have explained "v shall be an rvalue of a pointer to complete
class type" as "if v has a virtual table (pointer), that pointer shall not
be 0".

However, I've run into a case where it seems necessary to check whether
the virtual table pointer is 0 (instead of core dump):

#include <iostream>
class A { public: virtual int foo(void) { return 0; } }; // Some unrelated class
int main(void)
{
  ostream look_this_is_a_complete_class_I_can_create_it;
  ostream* os = &cerr;	// No warnings: `cerr is an ostream' (?)
  if (!dynamic_cast<A*>(os))
    cout << "It works" << endl;
  return 0;
}


~/c++/tests>g++ -g dc2.cc
~/c++/tests>a.out
Segmentation fault (core dumped)

~/c++/tests>gdb a.out core
GNU gdb 4.17.0.11 with Linux support
...
#0  0x8048887 in main () at dc2.cc:7
7         if (!dynamic_cast<A*>(os))
(gdb) p *os
$1 = {<ios> = {<_ios_fields> = {_strbuf = 0x40050640, _tie = 0x8049c38, 
      _width = 0, _flags = 8209, _fill = 32, _state = 0 '\000', 
      _exceptions = 0 '\000', _precision = 6, _arrays = 0x0}, _vptr. = 0x0}, 
  _vb.ios = 0x8049bfc}


Note that the _vptr. is 0x0.

Is this a bug in gcc? (ie, should you test if _vptr. is 0x0?), or
is there something seriously wrong with `cerr'?

Thanks for your time in advance,
Carlo Wood


PS This is unrelated to the two other problems with dynamic_cast
   that I just posted to gcc-bugs.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic