template specialization problem

Nathan Sidwell nathan@cs.bris.ac.uk
Mon Aug 3 14:45:00 GMT 1998

Mutually recusive template functions cannot be specialized. Some other
cases are problematical too.

The example I came across was slightly simpler, in that I was taking the
address of a template function, and then specializing it. That worked in
1.0.3a, but fails in the 19980727 snapshot. Luckily for me, a simple
source code reordering fixes the problem, but not so for the mutually
recursive/referential case.

Here is an example. There are two separate cases of this problem. The
first can be cured by reordering A<unsigned>::d and A<unsigned>::Fn. The
second case involving A<unsigned>::Fn1 and A<unsigned>::Fn2 can't be so

--begin tplspec.ii
typedef void (*C)();  
template<class T> struct A  
  static C d;  
  static void Fn();  
  void Fn1();  
  void Fn2();  
template<class T> C A<T>::d = &Fn; 
template<class T> void A<T>::Fn(){} 
template<class T> void A<T>::Fn1(){Fn2();}  
template<class T> void A<T>::Fn2(){Fn1();}  

template<> C A<unsigned>::d = &Fn;   
template<> void A<unsigned>::Fn(){}  
template<> void A<unsigned>::Fn1(){Fn2();}  
template<> void A<unsigned>::Fn2(){Fn1();}  
template class A<unsigned>;
--end tplspec.ii

Here's the session log. I'm using -fno-implicit-templates, and
explicitly instantiating the template, but the problem occurs without
the flag and having g++ implicitly instantiate things.

--begin log
nathan@laie:11621>uname -a
SunOS laie 5.5.1 Generic sun4u sparc SUNW,Ultra-1

nathan@laie:11622>egcs-0727-g++ -v -fno-implicit-templates -c tplspec.ii 
Reading specs from
gcc version egcs-2.91.52 19980727 (gcc2 ss-980609 experimental)

tplspec.ii -quiet -version -fno-implicit-templates -o
GNU C++ version egcs-2.91.52 19980727 (gcc2 ss-980609 experimental)
(sparc-sun-solaris2.5.1) compiled by GNU C version 2.8.1.
tplspec.ii:16: specialization of A<unsigned int>::Fn<unsigned int>()
after instantiation
tplspec.ii:16: explicit specialization of A<unsigned int>::Fn<unsigned
int>() after first use
tplspec.ii:18: specialization of A<unsigned int>::Fn2<unsigned int>()
after instantiation
tplspec.ii:18: explicit specialization of A<unsigned int>::Fn2<unsigned
int>() after first use
--end log

The error messages would be more helpful if they indicated where the
function was originally instantiated -- luckily for me it was the
previous definition.

The inability to specialize mutually recursive template functions,
although a rather esoteric use, leads me to consider this is a bug.

Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk

More information about the Gcc-bugs mailing list