This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Compiler chokes on a simple template - why?
- From: "Giovanni Bajo" <giovannibajo at libero dot it>
- To: "Topi Maenpaa" <topiolli at ee dot oulu dot fi>
- Cc: <gcc at gcc dot gnu dot org>
- Date: Thu, 17 Mar 2005 11:03:53 +0100
- Subject: Re: Compiler chokes on a simple template - why?
- References: <200503171033.54907.topiolli@ee.oulu.fi>
Topi Maenpaa <topiolli@ee.oulu.fi> wrote:
> ---------------------------------------------------
> template <class T> class A
> {
> public:
> template <class U> void test(T value) {}
> };
>
> template <class T> void test2(A<T>& a, T val)
> {
> a.test<int>(val);
> }
>
> int main()
> {
> A<int> a;
> a.test<int>(1); //works fine
> }
> ---------------------------------------------------
This is ill-formed. You need to write:
a.template test<int>(val);
because 'a' is a dependent name.
> The funny thing is that if I change the name of the "test2" function
> to "test", everything is OK. The compiler complains only if the
> functions have different names. Why does the name matter?
This is surely a bug. Would you please file a bug report about this?
> The code compiles if "test2" is not a template function. Furthermore,
> calling A<T>::test directly from main rather than through the
> template function works fine.
This is correct, because if "test2" is not a template function name anymore,
then 'a' is not a dependent name, and the 'template' keyword is not needed to
disambiguate the parser.
Giovanni Bajo