This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/47940] New: can call a pure virtual from a constructor/destructor
- From: "mlg7 at yandex dot ru" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 1 Mar 2011 10:09:58 +0000
- Subject: [Bug c++/47940] New: can call a pure virtual from a constructor/destructor
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47940
Summary: can call a pure virtual from a constructor/destructor
Product: gcc
Version: 4.4.5
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: mlg7@yandex.ru
A colleague of mine have been examining a crash that
boils down to the following:
--hellobug.cpp--
class Base {
public:
Base() { usefunc(); }
virtual void func()=0;
void usefunc() { func(); }
};
class Derived: public Base {
public:
virtual void func() {}
};
int main() {
Derived d;
return 0;
}
--eof--
$ g++ -Wall -Wextra hellobug.cpp
{just nothing}
$ ./a.out
pure virtual method called
terminate called without an active exception
Aborted
That is, "you asked for it, you got it" [1].
==
[1] from "Real Programmers Don't"
==
A variation:
==byebug.cpp==
#include <stdio.h>
class Base {
public:
~Base() { usefunc(); }
virtual void func()=0;
void usefunc() { func(); }
};
class Derived: public Base {
public:
virtual void func() {}
};
int main() {
Derived d;
printf("life is good\n");
return 0;
}
==eof==
$ g++ -Wall -Wextra byebug.cpp
{again nothing}
$ ./a.out
life is good
pure virtual method called
terminate called without an active exception
Aborted
I have to note that in a big software project,
the mistake is not so obvious.
But what can be done?
Functions that call pure virtual functions cannot
be called from constructors and destructors.
This may be discovered at compile-time, and the above
text makes a good error/warning message.
WHAT I GET: no warning
WHAT I WOULD EXPECT: a warning or an error message:
"Functions that call pure virtual functions cannot
be called from constructors and destructors"
$ gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.4 --enable-shared --enable-multiarch
--enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls
--with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc
--disable-werror --with-arch-32=i686 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)