views:

534

answers:

9

When overloading operators, is it necessary to overload >= <= and !=?

It seems like it would be smart for c++ to call !operator= for !=, !> for operator<= and !< for operator>=.

Is that the case, or is it necessary to overload every function?

+1  A: 

No, you only need to overload operator == and operator <, the standard library will take care of the rest :)

(EDIT: see using namespace std::rel_ops ;)

Autopulated
Don't think this is so... If it was true why would this page give example of defining != in terms of ==?http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html
Harley Green
Thanks for the clarification.
Harley Green
No it won't, you have to explicitly define each operator.
Igor Zevaka
@Autopulated: That one using directive is not enough, you need individual using declarations for each operator to introduce the names into the namespace so that they can be found through ADL.
Roger Pate
A: 

There are a few shortcuts you can use, such as CRTP to get them automatically injected (see the various Compare classes) into your class.

Roger Pate
I wonder why this and stakx's now-deleted answer were down-voted.
GMan
Beats me, I gave up worrying about rep long ago, but I still see people playing games trying to influence answers.
Roger Pate
Please explain reasons when you downvote.
Nicolás
+7  A: 

Boost operators might be what you are looking for. These will derive most of your operators based on a few fundamental ones.

That C++ does not provide this automatically makes sense, as one could give totally different meanings to < and >, for example (although it would often be a bad idea).

small_duck
C++ does provide this as std::rel_ops, but it's hard to use correctly and possibly shorter to just type them out yourself instead (avoiding the confusion).
Roger Pate
A: 

Yes! They are each technically different operators. C++ compilers are not inherently inference engines, they are parsers/compilers. They will only do as much as you say to do. http://www.parashift.com/c++-faq-lite/operator-overloading.html, http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Harley Green
Except they are inference engines! Look at ADL and figure out the function overload resolution rules for a good long headache.
Roger Pate
I suspect there would be a lot of AI researchers who would disagree with you on the technical definition of an inference engine in computer science. But yes, what modern compilers can "figure out" is impressive.
Harley Green
@Harley: The definition I'm using: "An inference engine is a form of finite state machine consisting of three actions: matching, selecting, and executing rules." The rules are simply a combination of function overload resolution and the declarations of functions and types in the program. No argument that it's primitive, of course.
Roger Pate
+1  A: 

I am going to take a minority viewpoint here. If you already use boost then using boost operators is not that big of a deal. It may be the correct and tested way to do things but adding boost dependency just for the operators is an overkill.

It is possible to write complex C++ programs without boost (which I personally find aesthetically unpleasant) and so to Keep It Simple (Stupid), to answer OP's question, if you overload operator ==, you should also overload operator !=. Same is true for <, >, ++ etc.

Igor Zevaka
If you don't like boost, for whatever reason, that doesn't mean you can't use the same technique. My answer contains a link to a single, self-contained header I wrote as an example of how to do exactly that in this case.
Roger Pate
I am just answering the question - C++ does not automatically call `operator ==` and invert the result when `!=` is not defined. Not disputing that there is an automagic way to do that.
Igor Zevaka
+2  A: 

Yes, it is necessary, if you want all of them to work the way you want them to work.

C++ does not force any specific semantics on most of the overloadable operators. The only thing that is fixed is the general syntax for the operator (including being unary or binary and things like precedence and associativity). This immediately means that the actual functionality that you implement in your overload can be absolutely arbitrary. In general case there might not be any meaningful connection between what operator == does and what operator != does. Operator == might write data to a file, while operator != might sort an array.

While overloading operators in such an arbitrary fashion is certainly not a good programming practice, the C++ language cannot assume anything. So, no, it cannot and will not automatically use ! == combination in place of !=, or ! > combination in place of <=.

AndreyT
Your example for `==` and `!=` is rather contrived. A more realistic one would be a class representing a SQL object, where you have to deal with the special semantics of `NULL`.
dan04
+1  A: 

Yes it is necessary to overload whichever operators you want to be used as you define them - C++ will not make the decision you describe above; however, keep in mind that if the reason you are overloading is to sort your class, than you only need to override the operators used by the sort routine. In the case of the RTL sort algorithm you only need to override < and =.

fupsduck
A: 
anjali gulati
A: 

C++ does not, as a language, define any operator in terms of any other overloaded operator. Just because you have operator+, doesn't mean you get operator+= for free (unlike Ruby and Scala). Just because you have operator < and == doesn't mean you get <= for free. Just because you have operator == doesn't mean you get != for free (unlike Ruby and Scala). Just because you have unary operator * (unary) doesn't mean you get operator -> for free.

std::relops (if imported correctly) and provide default definitions, and Boost provides some mix-ins that define all of the comparison operators in terms of < and ==.

Ken Bloom