operator != not automatically defined when operator == is defined
Observe:
class MyClass
{
public :
MyClass ( int x ) : i ( x ) {}
bool operator == ( const MyClass & other )
{ return i == other . i ; }
private :
int i ;
} ;
int main ( void )
{
MyClass a ( 1 ), b ( 2 ) ;
if ( a != b ) {}
}Trying to compile this gives:
operator-notequal.cpp:14: error: no match for ‘operator!=’ in ‘a != b’
Now there need conceivably be no definition for operator != other than the not-ted value of what operator == gives, so why does gcc want me to define operator != separately?
Similarly even if I define only operator !=, gcc will tell me to separately define operator == if I want to use that.
For the pairs:
== and !=
Quoted 1 line
and <=
< and >=
it is mathematically illogical to have any definition for each operator in the pair which does not output same as the not-ted output of the other operator in the pair, but gcc will not allow me to use an operator unless it has been explicitly defined even if its logical negative operator has been defined. I believe this is a defect in gcc.
Your comments?
Shriramana Sharma.
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 11 lines · Shriramana Sharma
operator-notequal.cpp:14: error: no match for ‘operator!=’ in ‘a != b’
Now there need conceivably be no definition for operator != other than the not-ted value of what operator == gives, so why does gcc want me to define operator != separately?
Similarly even if I define only operator !=, gcc will tell me to separately define operator == if I want to use that.
For the pairs:
Quoted 7 lines
== and !=
Quoted 1 line
and <=
< and >=
it is mathematically illogical to have any definition for each operator in the pair which does not output same as the not-ted output of the other operator in the pair,
In terms of mathematics, that's not even remotely true, particularly for the inequalities (totally-ordered vs partially-ordered).
In C/C++ terms, it isn't necessarily true for the standard types; e.g. if floating-point values x and y are both NaN, then both x==y and x!=y are false.
Signature · 5 lines
Glynn Clements <[email protected]> - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html