problem with template specialization in 1.1.2

Mike Haertel mike@ducky.net
Sat Mar 20 09:02:00 GMT 1999


Under EGCS 1.1.2 (built on FreeBSD 3.1, from the FreeBSD ports collection)
the following program fails to compile.  If -DWORKAROUND is added to the
command line it compiles.

The problem: apparently when compiling the specialization Bar<int>, the
compiler somehow manages to convince itself that the template function
foo<int>(int) has already been instantiated, when in fact it hasn't.  Then the
subsequent code that defines foo<int>(int) gets turned into an error.

A forward declaration of the foo<int>(int) function somehow manages to
prevent the compiler from imagining that foo<int>(int) has already
been defined.

$ eg++ -v
Reading specs from /usr/local/lib/gcc-lib/i386-portbld-freebsd3.1/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)

$ eg++ -c bug.cc
bug.cc:27: specialization of foo<int>(int) after instantiation

$ eg++ -DWORKAROUND -c bug.cc
$ 

--bug.cc--
template <class TYPE> void foo(TYPE);

template <class TYPE>
class Bar {
public:
	void bar(TYPE x) {
		foo(x);
	}
};

#ifdef WORKAROUND
template <> void foo<int>(int);
#endif

// specialization for int
template <>
class Bar <int> {
public:
	void bar(int x) {
		foo(x + 1);
	}
};

template <>
void
foo (int x)
{
}


More information about the Gcc-bugs mailing list