16

Given two identical boost::variant instances a and b, the expression ( a == b ) is permitted.

However ( a != b ) seems to be undefined. Why is this?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • In my sense its because `==` is needed for `map` contenance, (as well as `hash_value`) but operator != is luxury :) – v.oddou Apr 15 '15 at 01:15

3 Answers3

12

I think it's just not added to the library. The Boost.Operators won't really help, because either variant would have been derived from boost::operator::equality_comparable. David Pierre is right to say you can use that, but your response is correct too, that the new operator!= won't be found by ADL, so you'll need a using operator.

I'd ask this on the boost-users mailing list.

Edit from @AFoglia's comment:

Seven months later, and I'm studying Boost.Variant, and I stumble over this better explanation of the omission lists.

http://boost.org/Archives/boost/2006/06/105895.php

operator== calls operator== for the actual class currently in the variant. Likewise calling operator!= should also call operator!= of the class. (Because, theoretically, a class can be defined so a!=b is not the same as !(a==b).) So that would add another requirement that the classes in the variant have an operator!=. (There is a debate over whether you can make this assumption in the mailing list thread.)

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
AFoglia
  • 7,968
  • 3
  • 35
  • 51
  • 5
    Seven months later, and I'm studying Boost.Variant, and I stumble over this better explanation of the omission http://lists.boost.org/Archives/boost/2006/06/105895.php . Operator== calls operator== for the actual class currently in the variant. Likewise calling operator!= should also call operator!= of the class. (Because, theoretically, a class can be defined so a!=b is not the same as !(a==b).) So that would add another requirement that the classes in the variant have an operator!=. (There is a debate over whether you can make this assumption in the mailing list thread.) – AFoglia Feb 01 '10 at 16:43
3

This is a link to the answer from the author himself when this question was formulated on boost mailing list

Summarizing it, in the author opinion, implementing comparison operators (!= and <) would add more requirements on the types used to create the variant type.

I don't agree with his point of view though, since != can be implemented in the same way as ==, without necessarily hiding the possible implementations of these operators for each of the types making up the variant

lurscher
  • 25,930
  • 29
  • 122
  • 185
2

Because it doesn't need to.

Boost has an operators library which defines operator!= in term of operator==

David Pierre
  • 9,459
  • 4
  • 40
  • 32
  • 2
    I could be wrong. But if variant uses the operators library, doesn't that mean that a != b *should* work? I think what he wants is using std::rel_ops instead: { using std::rel_ops::operator!=; getA() != getB(); } – Johannes Schaub - litb Jun 25 '09 at 15:24
  • I didn't meant to say variant is using the lib itself, but that you can do it yourself to inject operator!= – David Pierre Jun 25 '09 at 15:49
  • So the expectation is to include an additional header and add a using declaration in source files where != is desired? – Drew Dormann Jun 25 '09 at 17:05
  • @JohannesSchaub-litb: it is also good to diminish inter dependencies between libraries, and let the clients bind the features together at a higher level if they need it, when its easy. but let us make so that `bcp` will extract the less files as possible. the compiler configuration part of boost alone already pulls one thousand files. – v.oddou Apr 15 '15 at 01:14