Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does boost::variant not provide operator !=

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?

like image 926
Drew Dormann Avatar asked Jun 25 '09 15:06

Drew Dormann


People also ask

What is boost:: variant?

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.

How boost variant works?

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

What is boost :: Apply_visitor?

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.


1 Answers

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

like image 197
AFoglia Avatar answered Sep 16 '22 19:09

AFoglia