6

I need to compare two variables of type boost::variant and I want to compare the values inside the variant for equality.

What would be the best way to implement this?

My variant looks like this:

typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant;
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415

1 Answers1

11

From variant docs:

EqualityComparable: variant is itself EqualityComparable if and only if every one of its bounded types meets the requirements of the concept.

So variant already implements equality if all the types are comparable. So in your case you should be able to just use the operator ==.

In general, you can implement a binary visitor by creating a unary visitor that encloses a reference to one of the arguments and applying the visitor to the second argument.

Giuseppe Ottaviano
  • 4,533
  • 2
  • 18
  • 18
  • 1
    Though this is not a real problem, with boost 1.45 and VC100 "==" works but "!=" works not an gives a compiler error (" no operator found ..."). – Semjon Mössinger Jan 11 '17 at 10:39