* operator != not automatically defined when operator == is defined
@ 2007-05-24 13:02 Shriramana Sharma
2007-05-25 22:32 ` Glynn Clements
0 siblings, 1 reply; 2+ messages in thread
From: Shriramana Sharma @ 2007-05-24 13:02 UTC (permalink / raw)
To: Linux C Programming List
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 !=
> 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 majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: operator != not automatically defined when operator == is defined
2007-05-24 13:02 operator != not automatically defined when operator == is defined Shriramana Sharma
@ 2007-05-25 22:32 ` Glynn Clements
0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2007-05-25 22:32 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> 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 !=
> > 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.
--
Glynn Clements <glynn@gclements.plus.com>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-05-25 22:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-24 13:02 operator != not automatically defined when operator == is defined Shriramana Sharma
2007-05-25 22:32 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).