473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

g++ template problem

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
|

Aug 11 '05 #1
5 1498
* sn********@gmail.com:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 11 '05 #2
sn********@gmail.com wrote:
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
Aug 11 '05 #3
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
|

Aug 12 '05 #4
sn********@gmail.com wrote:
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
Aug 12 '05 #5
>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
|

Aug 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.