C
CoolPint
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors
that I could not figure out. After reading other postings, I learned
that my coding was not compliant to the standard in the first place
and I did fix many of them, especially with the proper use of the
keyword "typename".
But I have one problem I have no idea how to fix. I created below a
simpler coding which demonstrates my problem:
The coding below used to compile and run fine under gcc 3.2.3, but
under 3.4.2, it doesn't compile with the error message "no match for
'operator==' in 'a==b'
What I read is that 3.4.2 is more compliant to the standard so I
believe it's my coding which is wrong, but I cannot figure out what is
wrong.
My guessing is that for some reason, the compiler doesn't "use" the
operator function template to instantiate a function to serve 'a ==
b'.
If I modify the friend declaration to include the definition of the
function inline, then it works as I guessed since each instantiation
of the class template will instantiate a corresponding global friend
function.
I will be very grateful if someone can kindly explain what the problem
is and how to fix the problem without making the function template
into an inlined global friend function I described above.
Thank you very much in advance.
template <typename T>
class Obj {
public:
class Sub;
};
template <typename T>
bool operator==(const typename Obj<T>::Sub & a, const typename
Obj<T>::Sub & b)
{ return a.n == b.n; }
template <typename T>
class Obj<T>::Sub {
friend bool operator==<T>(const Obj<T>::Sub &, const Obj<T>::Sub &);
public:
Sub(int i):n(i) { }
private:
int n;
};
#include <iostream>
int main()
{
Obj<int>::Sub a(10),b(10);
if ( a == b )
std::cout << "OK" ;
}
that I could not figure out. After reading other postings, I learned
that my coding was not compliant to the standard in the first place
and I did fix many of them, especially with the proper use of the
keyword "typename".
But I have one problem I have no idea how to fix. I created below a
simpler coding which demonstrates my problem:
The coding below used to compile and run fine under gcc 3.2.3, but
under 3.4.2, it doesn't compile with the error message "no match for
'operator==' in 'a==b'
What I read is that 3.4.2 is more compliant to the standard so I
believe it's my coding which is wrong, but I cannot figure out what is
wrong.
My guessing is that for some reason, the compiler doesn't "use" the
operator function template to instantiate a function to serve 'a ==
b'.
If I modify the friend declaration to include the definition of the
function inline, then it works as I guessed since each instantiation
of the class template will instantiate a corresponding global friend
function.
I will be very grateful if someone can kindly explain what the problem
is and how to fix the problem without making the function template
into an inlined global friend function I described above.
Thank you very much in advance.
template <typename T>
class Obj {
public:
class Sub;
};
template <typename T>
bool operator==(const typename Obj<T>::Sub & a, const typename
Obj<T>::Sub & b)
{ return a.n == b.n; }
template <typename T>
class Obj<T>::Sub {
friend bool operator==<T>(const Obj<T>::Sub &, const Obj<T>::Sub &);
public:
Sub(int i):n(i) { }
private:
int n;
};
#include <iostream>
int main()
{
Obj<int>::Sub a(10),b(10);
if ( a == b )
std::cout << "OK" ;
}