Possible bug in instanciation of friend template function.

Shinji Suzuki suzuki@wni.co.jp
Mon Oct 13 07:21:00 GMT 1997


In a similar vein to the problem reported by Mr. Andrey Slepuhin
under the title "problems with implicit template specialization",
egcs seems to forget to instanciate a template function that is
declared to be a friend to a template class. The following code
should illustrates the problem. When the friend declaration is
not uncommented the linker can't find the instance of the template
function.

-shinji

template <class T> class A;
template <class T> T * f(A<T>&);

template <class T>
class A
{
public:
  T* a;
  
  A (T* p) { a=p; }
 ~A () {}
 
  friend T* f(A&); // uncommending this decl eliminate the linker error
};

template <class T>
T * f(A<T>& x)
{
  return x.a;
}

int main()
{
  int x=2;
  A<int> y(&x);
  f(y);
  return 0;
}


[suzuki@micron /tmp]$ !gc
gcc -g -v tmp.cpp
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12/specs
gcc version egcs-2.90.12 971008 (gcc2-970802 experimental)
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -D__GNUC_MINOR__=90 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -g -Di386 -Di686 -Asystem(unix) -Acpu(i386) -Amachine(i386) -D__i386__ -D__i686__ -Asystem(unix) -Acpu(i386) -Amachine(i386) tmp.cpp /tmp/cca15505.ii
GNU CPP version egcs-2.90.12 971008 (gcc2-970802 experimental) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include/g++
 /usr/local/include
 /usr/local/i686-pc-linux-gnulibc1/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12/include
 /usr/include
End of search list.
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12/cc1plus /tmp/cca15505.ii -quiet -dumpbase tmp.cc -g -version -o /tmp/cca15505.s
GNU C++ version egcs-2.90.12 971008 (gcc2-970802 experimental) (i686-pc-linux-gnulibc1) compiled by GNU C version egcs-2.90.12 971008 (gcc2-970802 experimental).
 as -V -Qy -o /tmp/cca155051.o /tmp/cca15505.s
GNU assembler version 2.7 (i586-unknown-linux), using BFD version 2.7.0.2
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12/ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.1 /usr/lib/crt1.o /usr/lib/crti.o /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12/crtbegin.o -L/usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12 -L/usr/local/i686-pc-linux-gnulibc1/lib -L/usr/local/lib /tmp/cca155051.o -lgcc -lc -lgcc /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.90.12/crtend.o /usr/lib/crtn.o
/tmp/cca155051.o: In function `A<int>::~A(void)':
/tmp/tmp.cpp:11: undefined reference to `f(A<int> &)'
collect2: ld returned 1 exit status
[suzuki@micron /tmp]$ 





More information about the Gcc-bugs mailing list