bug in gcc?

T

Timon Gehr

g++ fails to compile the following code snippet:
---
struct B{int foo;};

template<class T> int foo(T x){
int y = x.foo<1>(0);
return y;
}

int main(){
int c = foo(B());
}
---
---
bug.cpp: In function ‘int foo(T) [with T = B]’:
bug.cpp:10:17: instantiated from here
bug.cpp:5:20: error: ‘foo’ is not a member template function
---

The code compiles if the template function is renamed from 'foo' to
'bar'.

Is this a bug?
 
A

Alf P. Steinbach

g++ fails to compile the following code snippet:
---
struct B{int foo;};

template<class T> int foo(T x){
int y = x.foo<1>(0);
return y;
}

int main(){
int c = foo(B());
}
---
---
bug.cpp: In function ‘int foo(T) [with T = B]’:
bug.cpp:10:17: instantiated from here
bug.cpp:5:20: error: ‘foo’ is not a member template function
---

The code compiles if the template function is renamed from 'foo' to
'bar'.

Is this a bug?

If it's true that it compiles with the simple renaming, then yes.

However, your posting is rather unclear.

Why on Earth are you showing the non-problematic code instead of the
code where there is possibly a problem?

Given the lack of logic in the posting, I think it's more likely that
you're confused about this.

I.e., a PEBKAC problem.


Cheers & hth.,

- Alf
 
T

Timon Gehr

g++ fails to compile the following code snippet:
template<class T>  int foo(T x){
     int y = x.foo<1>(0);
     return y;
}
int main(){
     int c = foo(B());
}
---
---
bug.cpp: In function ‘int foo(T) [with T = B]’:
bug.cpp:10:17:   instantiated from here
bug.cpp:5:20: error: ‘foo’ is not a member template function
---
The code compiles if the template function is renamed from 'foo' to
'bar'.
Is this a bug?

If it's true that it compiles with the simple renaming, then yes.

However, your posting is rather unclear.

I disagree.
Why on Earth are you showing the non-problematic code instead of the
code where there is possibly a problem?

I am showing the problematic code. I think it is valid C++. GCC
rejects it. The code where 'foo' is replaced by 'bar' is not
problematic: It is valid C++ and GCC compiles it.
Given the lack of logic in the posting, I think it's more likely that
you're confused about this.

I don't know, but I think it is possible that you are confused because
x.foo<1>(0) looks like a template function instantiation. I thought
that was the compiler's issue too, but I was wrong: I have managed to
reduce the code to a handy one-liner that does not contain it.

---
template<class T> bool foo(T x){return x.foo<1;}
---
---
bug.cpp: In function ‘bool foo(T)’:
bug.cpp:1:42: error: parse error in template argument list
---

Again, renaming the template function makes the code compile.
I.e., a PEBKAC problem.

Cheers & hth.,

- Alf

Thank you.
 
R

Rui Maciel

Alf said:
If it's true that it compiles with the simple renaming, then yes.

However, your posting is rather unclear.

Why on Earth are you showing the non-problematic code instead of the
code where there is possibly a problem?

The example code, when compiled with GCC 4.6.1, generates the exact same
error message which was presented by Timon. Therefore, it does appear to
fit the definition of problematic code.


Rui Maciel
 
J

jacob navia

Le 23/12/11 12:04, Timon Gehr a écrit :
struct B{int foo;};

template<class T> int foo(T x){
int y = x.foo<1>(0);
return y;
}

int main(){
int c = foo(B());
}

It is specified in the Gnu standard paragraph 6.6.6:

"Note: Gnu is not foo. Thou shalt not use foo in your programs"

Confirmed for

gcc version 4.2.1 (Apple Inc. build 5664)

in my Mac.
 
R

Rui Maciel

Marco said:
To me this looks like the problematic code :).

When we have the template function named bar the line:
int y = x.foo<1>(0);

is interpreted as:
int y = (x.foo<1)>(0);

What is not clear to me is why naming foo the template function change
this behavior.

Probably it's an implicit template instantiation thing. If the definition
of class T is unavailable and the template function invokes x.foo<1>(0),
then maybe it is assumed that class T has a template member function whose
identifier is foo, instead of the code representing a convoluted set of
comparissons between ints.


Rui Maciel
 
T

Timon Gehr

Probably it's an implicit template instantiation thing.  If the definition
of class T is unavailable and the template function invokes x.foo<1>(0),
then maybe it is assumed that class T has a template member function whose
identifier is foo, instead of the code representing a convoluted set of
comparissons between ints.

Rui Maciel

I don't think that holds:
 
J

Johannes Schaub

Timon said:
g++ fails to compile the following code snippet:
---
struct B{int foo;};

template<class T> int foo(T x){
int y = x.foo<1>(0);
return y;
}

int main(){
int c = foo(B());
}
---
---
bug.cpp: In function ‘int foo(T) [with T = B]’:
bug.cpp:10:17: instantiated from here
bug.cpp:5:20: error: ‘foo’ is not a member template function
---

The code compiles if the template function is renamed from 'foo' to
'bar'.

Is this a bug?

GCC behavior is incorrect. It should accept both.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top