1

If I define operator== and operator=< all other operators can be logically implied.

enter image description here

enter image description here

enter image description here

Does the compiler handle this for me or do I have to write them on my own?

ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
  • Not a duplicate. Its about if and why, not how. – ManuelSchneid3r Feb 15 '14 at 12:10
  • You have to do it yourself. C++ does not handle it for you. You *could* verify that just by trying to implement == and < and see if > or != would give you the relevant results. Asking for references is pointless, IMO, when you can verify something by experience very easily. – ThunderGr Feb 15 '14 at 12:22
  • The compiler is pretty restricted in what it can and cannot do for you by the requirement to follow the rules of the language. – Kerrek SB Feb 15 '14 at 12:30

2 Answers2

1

No it doesn't, you'll have to overload those as well if you plan to use them.

It doesn't even know that a != b is actually !(a==b).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Any references or do I just have to believe that? – ManuelSchneid3r Feb 15 '14 at 12:08
  • @ManuelSchneid3r You can easily try it out.. – Lan Pac Feb 15 '14 at 12:09
  • 2
    @ManuelSchneid3r I doubt the standard clearly points out that there's any relation, but it can be inferred through the lack of such a statement. – Luchian Grigore Feb 15 '14 at 12:10
  • 1
    @ManuelSchneid3r The C++ standard. Note there is [`std::rel_ops`](http://en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp) which is considered to be badly broken and so discouraged. You may want to have a look at [`boost::operators`](http://www.boost.org/doc/libs/1_55_0/libs/utility/operators.htm). – juanchopanza Feb 15 '14 at 12:10
0

No; the standard would have to assume that you wish for all your operators to follow this branch of mathematics' rules, and it has no right doing that.

However, in some situations, rules like this are used to simplify implementation. For example, the default comparator for std::map keys is std::less — where the underlying tree implementation needs to know whether key A is greater than or equal to key B, the logic is reformed to whether key B is less than A; in this way, only one comparator is required.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055