views:

336

answers:

3

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

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

+1  A: 

Because it doesn't need to.

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

David Pierre
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
I didn't meant to say variant is using the lib itself, but that you can do it yourself to inject operator!=
David Pierre
So the expectation is to include an additional header and add a using declaration in source files where != is desired?
Shmoopty
+3  A: 

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.

AFoglia
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
A: 

(!(a == b)) == (a != b)

dragonfly