BOOST_CHECK fails to compile operator<< for custom types

前端 未结 4 1367
温柔的废话
温柔的废话 2020-12-30 11:43

I wrote this really trivial class so that it\'s clear what my problem is:

class A
{
public:
    int x;
    A(int y) {x=y;}
    bool operator==(const A &o         


        
4条回答
  •  醉梦人生
    2020-12-30 12:35

    There is a clean way starting Boost 1.64 to log user defined types via the customization points. The full documentation of this feature can be found here.

    An example from the documentation is given below. The idea is to define the function boost_test_print_type for the type you want to print, and to bring this function into the test case (found via ADL):

    #define BOOST_TEST_MODULE logger-customization-point
    #include 
    
    namespace user_defined_namespace {
      struct user_defined_type {
          int value;
    
          user_defined_type(int value_) : value(value_)
          {}
    
          bool operator==(int right) const {
              return right == value;
          }
      };
    }
    
    namespace user_defined_namespace {
      std::ostream& boost_test_print_type(std::ostream& ostr, user_defined_type const& right) {
          ostr << "** value of user_defined_type is " << right.value << " **";
          return ostr;
      }
    }
    
    BOOST_AUTO_TEST_CASE(test1)
    {
        user_defined_namespace::user_defined_type t(10);
        BOOST_TEST(t == 11);
    
        using namespace user_defined_namespace;
        user_defined_type t2(11);
        BOOST_TEST(t2 == 11);
    }
    

提交回复
热议问题