This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: template specialization problem
- To: nathan at cs dot bris dot ac dot uk
- Subject: Re: template specialization problem
- From: Mark Mitchell <mark at markmitchell dot com>
- Date: Mon, 3 Aug 1998 23:34:20 -0700
- CC: egcs-bugs at cygnus dot com
- References: <35C5E73B.4364@cs.bris.ac.uk>
- Reply-to: mark at markmitchell dot com
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>;
You complain that this results in the errors:
test.C:16: specialization of A<unsigned int>::Fn() after instantiation
test.C:16: explicit specialization of A<unsigned int>::Fn() after first use
test.C:18: specialization of A<unsigned int>::Fn2() after instantiation
test.C:18: explicit specialization of A<unsigned int>::Fn2() after first use
While you are correct in asserting that these messages would be more
helpful if they indicated where the previous instantiation had
occurred, you are incorrect in asserting that the error messages
themselves are in error. For example, the call to Fn2 in
A<unsigned>::Fn1 triggers the instantiation of Fn2.
The correct way to write what you want is like this:
template <class T> void f();
template <class T> void g();
template <> void f<int>();
template <> void g<int>();
template <> void f<int>() { g<int>(); }
template <> void g<int>() { f<int>(); }
That lets the compiler know that f<int> and g<int> are being
specialized before they are instantiated.
--
Mark Mitchell mark@markmitchell.com
Mark Mitchell Consulting http://www.markmitchell.com