I recently came across a subtle bug in my program that probably deserves a
warning from the compiler. It involves virtual functions in base/derived
classes where one is const and the other is not. The behavior under these
circumstances makes sense, but this situation should be easy to detect and
might deserve a warning. Here's a very simple sample program (the important
features are which functions are const and which are not):

class A {
public:
  virtual void sayhi() const {std::cout << "A says hi" << std::endl;};
  virtual void saybye() {std::cout << "A says bye" << std::endl;};
};

class A_noconst : public A {
public:
  void sayhi() {std::cout << "A_noconst says hi" << std::endl;};
  void saybye() {std::cout << "A_noconst says bye" << std::endl;};
};

class A_const : public A {
public:
  void sayhi() const {std::cout << "A_const says hi" << std::endl;};
  void saybye() const  {std::cout << "A_const says bye" << std::endl;};
};

int main() {
  A *p1,*p2;

  p1 = new A_noconst;
  p2 = new A_const;

  p1->sayhi(); p1->saybye();

  p2->sayhi(); p2->saybye();

  return 0;
}

The output is
A says hi
A_noconst says bye
A_const says hi
A says bye

Here's the output from g++ -v:
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)
 /usr/libexec/gcc/x86_64-redhat-linux/3.4.6/cc1plus -quiet -v -D_GNU_SOURCE
samplebug.cpp -quiet -dumpbase samplebug.cpp -mtune=k8 -auxbase samplebug -Wall
-version -o /tmp/cc8IHZGe.s
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6

/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../include/c++/3.4.6/backward
 /usr/local/include
 /usr/lib/gcc/x86_64-redhat-linux/3.4.6/include
 /usr/include
End of search list.
GNU C++ version 3.4.6 20060404 (Red Hat 3.4.6-9) (x86_64-redhat-linux)
        compiled by GNU C version 3.4.6 20060404 (Red Hat 3.4.6-9).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 as -V -Qy -o /tmp/ccjT2hSA.o /tmp/cc8IHZGe.s
GNU assembler version 2.15.92.0.2 (x86_64-redhat-linux) using BFD version
2.15.92.0.2 20040927
 /usr/libexec/gcc/x86_64-redhat-linux/3.4.6/collect2 --eh-frame-hdr -m
elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o samplebug
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/3.4.6
-L/usr/lib/gcc/x86_64-redhat-linux/3.4.6
-L/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64
-L/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../.. -L/lib/../lib64
-L/usr/lib/../lib64 /tmp/ccjT2hSA.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s
-lgcc /usr/lib/gcc/x86_64-redhat-linux/3.4.6/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/3.4.6/../../../../lib64/crtn.o


-- 
           Summary: const/nonconst virtual function ambiguity deserves a
                    warning
           Product: gcc
           Version: 3.4.6
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: petersamuelson at gmail dot com


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

Reply via email to