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?
Boost. Variant, part of collection of the Boost C++ Libraries. It is a safe, generic, stack-based discriminated union container, offering a simple solution for manipulating an object from a heterogeneous set of types in a uniform manner.
boost::variant is conceptually similar to what you've done before, but by not literally using a union and instead taking a manual approach to placement construction/destruction of objects in its buffer (while handling alignment issues explicitly) it works around the restrictions that C++ has re complex types in actual ...
boost::apply_visitor — Allows compile-time checked type-safe application of the given visitor to the content of the given variant, ensuring that all types are handled by the visitor.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With