https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87697

            Bug ID: 87697
           Summary: Casting a base class to derived gives no warning
           Product: gcc
           Version: 7.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jynelson at email dot sc.edu
  Target Milestone: ---

Created attachment 44881
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44881&action=edit
The preprocessed source

Casting a base class to a derived class gives no warning, even with -Wall
-Werror enabled. I've been told on IRC that this sort of cast is undefined
behaviour according to spec if neither class is virtual. If that is the case,
it would be nice to have a warning.

If that isn't the case, there are several more questions that arise, like `why
does c1 have access to c2` and `why can the const value c2::x be accessed
without be initialized`?

$ gcc -v       
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --with-isl --enable-libmpx
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686
--build=x86_64-redhat-linux
Thread model: posix
gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)
$ g++ -Wall -Werror inheritance.cc
$ ./a.out
constructor 1
c2: 0

Reply via email to