This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/38484] New: dynamic_cast fails to respect private base when cross casting


Here is the code:

#include <iostream>

struct Reader { virtual ~Reader() {} };
struct Writer: virtual Reader {};

void func(Reader *r)
{
  std::cout << dynamic_cast<Writer *>(r) << std::endl;
}

struct AccessorReader: virtual Reader {};
struct Accessor: AccessorReader, private Writer {};

int main()
{
  Accessor a;
  func(static_cast<AccessorReader *>(&a));
  return 0;
}


What happens is that the dynamic_cast in "func" succeeds to convert the Reader
* to a Writer *. This is unexpected due to the private inheritance of Writer in
Accessor.

If the virtual inheritance of Reader is changed to ordinary inheritance in both
places, the problem goes away, i.e. the dynamic_cast returns 0.

I am not entirely sure this is a bug, but I do know that dynamic_cast is
supposed to respect privacy, and as far as I can see, there is no way, in the
above code, that "func" can know that the Reader is also a Writer without
violating the privacy specification.

I'd be happy if someone could clarify this matter for me.

Version: gcc (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)


-- 
           Summary: dynamic_cast fails to respect private base when cross
                    casting
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: kristian dot spangsege at gmail dot com
 GCC build triplet: x86_64-redhat-linux
  GCC host triplet: x86_64-redhat-linux
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38484


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]