I'm having trouble with a templated comparison operator for comparing instances of a type dependent on the template argument. I've been scouring the tubes for a while, including SO, and struggling to understand what I'm doing wrong. I believe it's an issue with dependent name lookup, but that's not helping me get the code working :-(
Here's a minimal testcase:
template<typename T>
struct Foo
{
struct Bar {
T t;
};
Bar b;
};
template<typename T>
inline bool operator<(const typename Foo<T>::Bar& b1, const typename Foo<T>::Bar& b2)
{ return b1.t < b2.t; }
int main()
{
Foo<int> f1, f2;
return (f1.b < f2.b? 0 : 1);
}
Compilation gives:
templated-comparison-op.cpp: In function ‘int main()’:
templated-comparison-op.cpp:20:20: error: no match for ‘operator<’ in ‘f1.Foo<int>::b < f2.Foo<int>::b’
templated-comparison-op.cpp:20:20: note: candidate is:
templated-comparison-op.cpp:13:13: note: template<class T> bool operator<(const typename Foo<T>::Bar&, const typename Foo<T>::Bar&)
For now I've got the comparison operator as a member of the template class, which works fine, so no urgency. I would like to understand what I'm missing here though; can anyone enlighten me?
Edit: That's not actually an accurate testcase; in the real code I extracted this case from, template class Foo is nested inside another class, and this is why the 'typename' keywords are required. As answers below state, in the code above 'typename' would be unnecessary.
Edit: I've replaced the original testcase with a new one highlighting the current problem, and also updated the question title, since I'm struggling to replicate the exact error I get in the real codebase. If there's a workaround for the issue in the testcase then perhaps that'll get the equivalent in real code closer to compilation, so still interested in pursuing this.