Topic: STL overloads != operator of MyClass!
Author: Ildefonso Junquero <junquero@arrakis.es>
Date: 1997/04/06 Raw View
I'm using stl that comes whit gcc 2.7.2, and i get surprised because g++
takes precedence of stl templated operator != than a class operator !=.
Here is an example :
#include <vector>
#include <algo>
typedef enum MyBool { myFalse, myTrue };
friend ostream& operator<<(ostream&os, const MyBool &mb)
{ os << ((mb==myFalse) ? "False" : "True"); return os; }
class MyClass
{
public:
MyClass(int v=0) : a(v) {}
MyBool operator==(const MyClass& mc) const
{ return MyBool(a==mc.a); }
MyBool operator!=(const MyClass& mc) const
{ return MyBool(a!=mc.a); }
protected:
int a;
};
main()
{
cout << (MyClass(3) == MyClass(3)) << endl;
cout << (MyClass(3) != MyClass(3)) << endl;
}
The output is :
True
0 <<--- I expected False!
Can someone explain me why?, Is this an g++ bug?
It works like i expected in Borland C++ 4.52 .
What does the standrad says?
Thanks!
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Marcelo Cantos <marcelo@mds.rmit.edu.au>
Date: 1997/04/06 Raw View
Ildefonso Junquero <junquero@arrakis.es> writes:
> I'm using stl that comes whit gcc 2.7.2, and i get surprised because g++
> takes precedence of stl templated operator != than a class operator !=.
> Here is an example :
>
> #include <vector>
> #include <algo>
> typedef enum MyBool { myFalse, myTrue };
>
> friend ostream& operator<<(ostream&os, const MyBool &mb)
^^^^^^ this is illegal.
A function can't declare itself to be the friend of a class.
This the class's prerogative.
> [snip, snip]
> The output is :
>
> True
> 0 <<--- I expected False!
>
> Can someone explain me why?, Is this an g++ bug?
> It works like i expected in Borland C++ 4.52 .
FWIW: The Sun compiler (4.2) produces the correct output.
> What does the standrad says?
Class members should override global functions.
13.3.3 Best Viable Function [over.match.best]
1 [Paraphrased, F1 beats F2 if...]
...
--F1 is a non-template function and F2 is a template function special-
ization,...
--
______________________________________________________________________
Marcelo Cantos, Research Assistant __/_ marcelo@mds.rmit.edu.au
Multimedia Database Systems Group, RMIT / _ Tel 61-3-9282-2497
723 Swanston St, Carlton VIC 3053 Aus/ralia ><_> Fax 61-3-9282-2490
Acknowledgements: errors - me; wisdom - God; funding - RMIT
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Ildefonso Junquero <junquero@arrakis.es>
Date: 1997/04/07 Raw View
I'm using stl that comes whit gcc 2.7.2, and i get surprised because g++
takes precedence of stl templated operator != than a class operator !=.
Here is an example :
#include <vector>
#include <algo>
typedef enum MyBool { myFalse, myTrue };
friend ostream& operator<<(ostream&os, const MyBool &mb)
{ os << ((mb==myFalse) ? "False" : "True"); return os; }
class MyClass
{
public:
MyClass(int v=0) : a(v) {}
MyBool operator==(const MyClass& mc) const
{ return MyBool(a==mc.a); }
MyBool operator!=(const MyClass& mc) const
{ return MyBool(a!=mc.a); }
protected:
int a;
};
main()
{
cout << (MyClass(3) == MyClass(3)) << endl;
cout << (MyClass(3) != MyClass(3)) << endl;
}
The output is :
True
0 <<--- I expected False!
Can someone explain me why?, Is this an g++ bug?
It works like i expected in Borland C++ 4.52 .
What does the standrad says?
Thanks!
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: lijewski@mothra.lbl.gov (Mike Lijewski)
Date: 1997/04/09 Raw View
It's a bug in g++ -- the templatized relational operators in <utility> are
wrongly getting precedence over class-specific ones. I reported it some
time ago and have recently received word that it'll be fixed in 2.8.
In article <3347DF01.7767DD8B@arrakis.es>,
Ildefonso Junquero <junquero@arrakis.es> wrote:
>I'm using stl that comes whit gcc 2.7.2, and i get surprised because g++
>takes precedence of stl templated operator != than a class operator !=.
>
> example deleted ....
--
Mike Lijewski mjlijewski@lbl.gov
Center for Computational Sciences and Engineering phone: 510-486-8667
Lawrence Berkeley National Laboratory fax: 510-486-6900
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]