BOOST_CHECK_EQUAL与对< INT,INT>和运营商定制<< [英] BOOST_CHECK_EQUAL with pair<int, int> and custom operator <<

查看:123
本文介绍了BOOST_CHECK_EQUAL与对< INT,INT>和运营商定制<<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图做一个BOOST_CHECK_EQUAL(对,对)
GCC犯规找到流运营商对,inspite宣布吧。
有趣的是,性病::出来发现操作。

 的ostream&安培;运营商的LT;≤(ostream的&安培; S,常量对< INT,INT>&安培; P){
    小号所述&;&下; '<' << p.first<< ','&所述;&下; p.second<< '>';
    返回S;
}
BOOST_AUTO_TEST_CASE(作品)
{
    对< INT,INT>预期(5,5);
    对< INT,INT>实际(5,5);
    性病::法院LT&;<预计<<的std :: ENDL;
    性病::法院LT&;<实际<<的std :: ENDL;
    BOOST_CHECK(实际==预期);
}BOOST_AUTO_TEST_CASE(no_work)
{
    对< INT,INT>预期(5,5);
    对< INT,INT>实际(5,5);
    BOOST_CHECK_EQUAL(实际,预期);
}

这并不编译错误:

  ...从这里实例化
../boost-atp/release/include/boost/test/test_tools.hpp:326:9:错误:不对应的运营商的LT;<'在'OSTR<< T'


解决方案

运营商的LT;< STD Remus的答案是在C ++ 14草案(N4296部分:17.6.4.2.1)未定义的行为。加速提供了一个钩子(这个答案使用),你可以这样写:

 名字空间boost
{
    命名空间test_tools
    {
        模板< typename的T,typename的U>
        结构print_log_value<的std ::对< T,U> >
        {
            void运算符()(STD :: ostream的和放大器;操作系统的std ::对< T,U>常量和放大器; PR)
            {
                OS<< < <<的std ::获得℃的>(PR)LT;< ,&所述;&下;的std ::获得< 1 GT;(PR)LT;< >中;
            }
        };
    }
}

print_log_value 是一个模板,所以如果你不声明像对℃的模板值; T,U> ,你需要写类似:

 模板<>
结构print_log_value<&MyType的GT; {/ *在这里执行* /};

When attempting to do a BOOST_CHECK_EQUAL(pair, pair), gcc doesnt find the stream operator for pair, inspite of declaring it. The funny thing is that std::out finds the operator.

ostream& operator<<(ostream& s, const pair<int,int>& p) {
    s << '<' << p.first << ',' << p.second << '>';
    return s;
}


BOOST_AUTO_TEST_CASE(works)
{
    pair<int,int> expected(5, 5);
    pair<int,int> actual  (5, 5);
    std::cout << expected << std::endl;
    std::cout << actual   << std::endl;
    BOOST_CHECK(actual == expected);
}

BOOST_AUTO_TEST_CASE(no_work)
{
    pair<int,int> expected(5, 5);
    pair<int,int> actual  (5, 5);
    BOOST_CHECK_EQUAL(actual, expected);
}

This doesnt compile with the error:

...  instantiated from here
../boost-atp/release/include/boost/test/test_tools.hpp:326:9: error: no match for ‘operator<<’ in ‘ostr << t’

Putting operator<< in std like Remus's answer is undefined behavior in the C++ 14 draft (N4296 section:17.6.4.2.1). Boost provides a hook (used by this answer) and you can write:

namespace boost
{
    namespace test_tools
    {
        template<typename T,typename U>
        struct print_log_value<std::pair<T, U> >
        {
            void operator()(std::ostream& os, std::pair<T, U> const& pr)
            {
                os << "<" << std::get<0>(pr) << "," << std::get<1>(pr) << ">";
            }
        };
    }
}

print_log_value is a template so if you are not declaring a templated value like pair<T,U>, you will need to write something like:

template<>
struct print_log_value<MyType>{ /* implementation here*/ };

这篇关于BOOST_CHECK_EQUAL与对&LT; INT,INT&GT;和运营商定制&LT;&LT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆