g++ template problem

S

snorkelman

I'm having a compile time problem with g++ 3.2.something, which I can
boil down to this:

struct A {
template<int N> void foo(){}
};

template<class T>
void fn( int & ) {
A a;
a.foo<0>(); //compiler reports "syntax error before semicolon"
}

main()
{
A a;
a.foo<0>();
}

Note that the template function is not being instantiated - the error
arises from the function definition. It appears that the compiler is
confused about just what foo is.

But using it in main() gives me no problem - the compiler is well aware
at that point that foo<N> is a function.

There are a couple of workarounds that coerce everything into
compiling, but in my real code things are a little hairier and I
haven't been able to get them to work. I guess what I am hoping to find
out first is what I am doing wrong. Anything obvious?

|
| markn at ieee dot org
|
 
A

Alf P. Steinbach

* (e-mail address removed):
I'm having a compile time problem with g++ 3.2.something, which I can
boil down to this:

struct A {
template<int N> void foo(){}
};

template<class T>
void fn( int & ) {
A a;
a.foo<0>(); //compiler reports "syntax error before semicolon"
}

main()
{
A a;
a.foo<0>();
}

Compiles fine with g++ 3.4.2, MSVC 7.1 and Comeau 4.3.3 online.
 
V

Victor Bazarov

I'm having a compile time problem with g++ 3.2.something, which I can
boil down to this:

struct A {
template<int N> void foo(){}
};

template<class T>
void fn( int & ) {
A a;
a.foo<0>(); //compiler reports "syntax error before semicolon"
}

main()

int main()
{
A a;
a.foo<0>();
}

Note that the template function is not being instantiated - the error
arises from the function definition. It appears that the compiler is
confused about just what foo is.

It's quite possible that the compiler is confused, but both VC++ 2003 and
Comeau (online) compile it without a problem.
But using it in main() gives me no problem - the compiler is well aware
at that point that foo<N> is a function.

There are a couple of workarounds that coerce everything into
compiling, but in my real code things are a little hairier and I
haven't been able to get them to work. I guess what I am hoping to find
out first is what I am doing wrong. Anything obvious?

Using a buggy compiler counts?

V
 
S

snorkelman

In the more complex version of my problem, I think the solution to this
is that instead of writing:

a.foo<0>()

I need to write

a.template foo<0>()

I have to admit that I'm still a little fuzzy on this requirement.
Visual C++ has never seemed to need this, but g++ frequently thinks I
need to insert that keyword to clarify things.

If I follow it correctly, the explanation in the standard implies that
when I write:

a.foo<0>()

the compiler interprets a.foo as a declaration, then it sees a less
than sign and freaks out.

I don't completely get it, but I can apply the fix with aplomb.

|
| markn at ieee dot org
|
 
V

Victor Bazarov

In the more complex version of my problem, I think the solution to
this is that instead of writing:

a.foo<0>()

I need to write

a.template foo<0>()

I have to admit that I'm still a little fuzzy on this requirement.
Visual C++ has never seemed to need this, but g++ frequently thinks I
need to insert that keyword to clarify things.

If I follow it correctly, the explanation in the standard implies that
when I write:

a.foo<0>()

the compiler interprets a.foo as a declaration, then it sees a less
than sign and freaks out.

I don't completely get it, but I can apply the fix with aplomb.

The postfix expression 'a.foo' cannot be interpreted in any other way
than the use of the member template. That means that the opening '<'
cannot be interpreted as a 'less-than' sign. The use of 'template'
keyword apparently helps the compiler in time of need to understand
that 'foo' is a member template. But according to the Standard it is
only necessary if 'a' itself is a dependent expression. It isn't.

V
 
S

snorkelman

But according to the Standard it is
only necessary if 'a' itself is a dependent expression. It isn't.

Yeah, I think the g++ 3.2 problem is probably a compiler error. My
real-world problem was using a dependent expression, and is
appropriately solved by the use of T::template blah-blah.
|
| markn at ieee dot org
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,797
Messages
2,569,647
Members
45,378
Latest member
danzeev

Latest Threads

Top