virtual functions in local classes

Peter Osterlund peter.osterlund@mailbox.swipnet.se
Sun Sep 19 05:39:00 GMT 1999


The following code:

	class A {
	public:
	    virtual void f();
	};
	
	void A::f() {}
	
	void g()
	{
	    class B : public A {
	    public:
		void f() { }
	    };
	
	    B b;
	
	    b.f();
	}
	
	int main()
	{
	    g();
	    return 0;
	}

doesn't link with recent gcc versions. (egcs 1.2, gcc2.95.x gcc
2.96 fails, but gcc 2.7.2 compiles and links the code.) If f() is made
non-virtual the program compiles and links.

The standard says (in 3.5.8 [basic.link] of the 1996 DWP) that local
classes have no linkage, and that they shall not be used to declare an
entity with linkage. However, I couldn't find any evidence that this
implies that virtual functions are not allowed in local classes.

Is this a compiler bug, or did I miss something?

Here is the compiler output:

cd ~/test/cpp/
PATH=/home/petero/gcc295/bin:$PATH g++ -v -O2 -Wall -o test42 test42.C
Reading specs from /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19990906 (prerelease)
 /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/cpp -lang-c++ -v -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -D__OPTIMIZE__ -Wall -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -Di686 -Dpentiumpro -D__i686 -D__i686__ -D__pentiumpro -D__pentiumpro__ test42.C /tmp/ccXLqv4R.ii
GNU CPP version 2.95.2 19990906 (prerelease) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3
 /usr/local/include
 /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../i686-pc-linux-gnu/include
 /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/include
 /usr/include
End of search list.
The following default directories have been omitted from the search path:
End of omitted list.
 /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/cc1plus /tmp/ccXLqv4R.ii -quiet -dumpbase test42.cc -O2 -Wall -version -o /tmp/ccgyYa2K.s
GNU C++ version 2.95.2 19990906 (prerelease) (i686-pc-linux-gnu) compiled by GNU C version 2.95.2 19990906 (prerelease).
 as -V -Qy -o /tmp/ccGiVoWA.o /tmp/ccgyYa2K.s
GNU assembler version 2.9.1 (i386-redhat-linux), using BFD version 2.9.1.0.23
 /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o test42 /usr/lib/crt1.o /usr/lib/crti.o /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/crtbegin.o -L/home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2 -L/home/petero/gcc295/lib /tmp/ccGiVoWA.o -lstdc++ -lm -lgcc -lc -lgcc /home/petero/gcc295/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/crtend.o /usr/lib/crtn.o
/tmp/ccGiVoWA.o(.gnu.linkonce.d.__vt_Q27g__Fv.0_1B+0x8): undefined reference to `f__Q27g__Fv.0_1B.6'
collect2: ld returned 1 exit status

Compilation exited abnormally with code 1 at Sun Sep 19 14:16:58

-- 
Peter Österlund          Email:     peter.osterlund@mailbox.swipnet.se
Sköndalsvägen 35                    f90-pos@nada.kth.se
S-128 66 Sköndal         Home page: http://home1.swipnet.se/~w-15919
Sweden                   Phone:     +46 8 942647


More information about the Gcc-bugs mailing list