Question

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?

Was it helpful?

Solution

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.)

OTHER TIPS

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

Because it doesn't need to.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top