template constructors and virtual tables

Dimitri PAPADOPOULOS-ORFANOS orfanos@ima.enst.fr
Thu Mar 19 14:29:00 GMT 1998


Hi,

I can't see why the following piece of code wouldn't be legal, but
egcs-1.0.2 (and GNU 2.8.1 and egcs-1.0.1) won't link it. It combines
STL-like template constructors and inheritance.

// file A.h
//==========

	struct A {
		template <class InputIterator> A(InputIterator, InputIterator);
		virtual ~A();
	};

	template <class InputIterator>
	inline
	A::A(InputIterator first, InputIterator last) {
	}

// file B.h
//==========

	#include "A.h"
	
	struct B : public A {
		template <class InputIterator> B(InputIterator, InputIterator);
		virtual ~B();
	};

// file A.cc
//===========

	#include "A.h"
	
	A::~A() {
	}

// file B.cc
//===========

	#include "B.h"
	
	B::~B() {
	}

// file main.cc
//==============

	#include "B.h"
	#include <list>
	
	int main() {
		list<int> l;
		B b(l.begin(), l.end());
		return 0;
	}



Here are the link-time error messages:
	% g++ -o main main.cc A.cc B.cc
	/tmp/cca174002.o(.rodata+0x0): multiple definition of `A virtual table'
	/tmp/cca174001.o(.rodata+0x24): first defined here
	/tmp/cca174002.o: In function `A type_info function':
	/tmp/cca174002.o(.text+0x2c): multiple definition of `A type_info function'
	/tmp/cca174001.o(.text+0x13c): first defined here
	/usr/bin/ld: Warning: size of symbol `A type_info function' changed from 44 to 52 in /tmp/cca174002.o
	/tmp/cca174003.o(.rodata+0x0): multiple definition of `B virtual table'
	/tmp/cca174001.o(.rodata+0x18): first defined here
	/tmp/cca174003.o: In function `B type_info function':
	/tmp/cca174003.o(.text+0x24): multiple definition of `B type_info function'
	/tmp/cca174001.o(.text+0x104): first defined here
	/usr/bin/ld: Warning: size of symbol `B type_info function' changed from 56 to 64 in /tmp/cca174003.o
	collect2: ld returned 1 exit status

Must such code be considered illegal? In that case, I wish some more
understandable warning could be issued at compile-time.
Or is this a bug of the compiler?

Thank you,
--
Dimitri Papadopoulos



More information about the Gcc-bugs mailing list