Potential g++ template bug?
Posted
by
Evan Teran
on Stack Overflow
See other posts from Stack Overflow
or by Evan Teran
Published on 2011-01-13T07:27:32Z
Indexed on
2011/01/13
7:53 UTC
Read the original article
Hit count: 307
I've encountered some code which I think should compile, but doesn't. So I'm hoping some of the local standards experts here at SO can help :-).
I basically have some code which resembles this:
#include <iostream>
template <class T = int>
class A {
public:
class U {
};
public:
U f() const { return U(); }
};
// test either the work around or the code I want...
#ifndef USE_FIX
template <class T>
bool operator==(const typename A<T>::U &x, int y) {
return true;
}
#else
typedef A<int> AI;
bool operator==(const AI::U &x, int y) {
return true;
}
#endif
int main() {
A<int> a;
std::cout << (a.f() == 1) << std::endl;
}
So, to describe what is going on here. I have a class template (A
) which has an internal class (U
) and at least one member function which can return an instance of that internal class (f()
).
Then I am attempting to create an operator==
function which compares this internal type to some other type (in this case an int
, but it doesn't seem to matter).
When USE_FIX
is not defined I get the following error:
test.cc: In function 'int main()':
test.cc:27:25: error: no match for 'operator==' in 'a.A<T>::f [with T = int]() == 1'
Which seems odd, because I am clearly (I think) defining a templated operator==
which should cover this, in fact if I just do a little of the work for the compiler (enable USE_FIX), then I no longer get an error. Unfortunately, the "fix" doesn't work generically, only for a specific instantiation of the template.
Is this supposed to work as I expected? Or is this simply not allowed?
BTW: if it matters I am using gcc 4.5.2.
© Stack Overflow or respective owner