Question

I have a boost recursive variant as below. When I compare two recursive variant object using assert, it works fine but with EXPECT_EQ, it gives compile error.

typedef boost::make_recursive_variant<bool, boost::uint8_t, boost::uint32_t,
    boost::int32_t, double, std::string, boost::uuids::uuid>::type rvariant_type;

variant_type b1 = true;
rvariant_type b2 = true;

assert(b1 == b2);  //work fine

EXPECT_EQ(b1,b2); //gives compiler error.
EXPECT_EQ(boost::get<bool>(b1), boost::get<bool>(b2)); //works fine

boost/v1.46.1/include/boost/variant/detail/variant_io.hpp:64: error: no match for ‘operator<<’ in ‘((const boost::detail::variant::printer > >*)this)->boost::detail::variant::printer > >::out_ << operand’

Was it helpful?

Solution

gtest makes heavy use of streams for output but it seems that the boost::variant support for printing via an overloaded operator<< is very limited, if not nonexistant.

Take a look at this:

#include <boost/variant.hpp>
#include <boost/cstdint.hpp>
#include <boost/uuid/uuid.hpp>
#include <iostream>
typedef boost::make_recursive_variant<bool, boost::uint8_t, boost::uint32_t,
    boost::int32_t, double, std::string, boost::uuids::uuid>::type rvariant_type;

int main() {
  rvariant_type v1 = true;
  std::cout << v1 << std::endl;
  return 0;
}

This very short program gives the same compilation error you got from gtest.

Supplementing it with this:

std::ostream& operator<<(std::ostream& out, const rvariant_type& p) {
  return out << boost::get<bool>(p);
}

makes my test compile, I'll take a look if I can make your example work as well.

UPDATE: I just compiled and successfully ran a test based on your code after putting the above-mentioned operator<<, so the lack of an operator<< is precisely what was causing it.

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