BOOST_CHECK_EQUAL与对&LT; INT,INT&GT;和运营商定制&LT;&LT; [英] BOOST_CHECK_EQUAL with pair<int, int> and custom operator <<
问题描述
在试图做一个BOOST_CHECK_EQUAL(对,对)
GCC犯规找到流运营商对,inspite宣布吧。
有趣的是,性病::出来发现操作。
的ostream&安培;运营商的LT;≤(ostream的&安培; S,常量对&LT; INT,INT&GT;&安培; P){
小号所述&;&下; '&LT;' &LT;&LT; p.first&LT;&LT; ','&所述;&下; p.second&LT;&LT; '&GT;';
返回S;
}
BOOST_AUTO_TEST_CASE(作品)
{
对&LT; INT,INT&GT;预期(5,5);
对&LT; INT,INT&GT;实际(5,5);
性病::法院LT&;&LT;预计&LT;&LT;的std :: ENDL;
性病::法院LT&;&LT;实际&LT;&LT;的std :: ENDL;
BOOST_CHECK(实际==预期);
}BOOST_AUTO_TEST_CASE(no_work)
{
对&LT; INT,INT&GT;预期(5,5);
对&LT; INT,INT&GT;实际(5,5);
BOOST_CHECK_EQUAL(实际,预期);
}
这并不编译错误:
...从这里实例化
../boost-atp/release/include/boost/test/test_tools.hpp:326:9:错误:不对应的运营商的LT;&LT;'在'OSTR&LT;&LT; T'
把运营商的LT;&LT;
在 STD
如 Remus的答案是在C ++ 14草案(N4296部分:17.6.4.2.1)未定义的行为。加速提供了一个钩子(这个答案使用),你可以这样写:
名字空间boost
{
命名空间test_tools
{
模板&LT; typename的T,typename的U&GT;
结构print_log_value&LT;的std ::对&LT; T,U&GT; &GT;
{
void运算符()(STD :: ostream的和放大器;操作系统的std ::对&LT; T,U&GT;常量和放大器; PR)
{
OS&LT;&LT; &LT; &LT;&LT;的std ::获得℃的&GT;(PR)LT;&LT; ,&所述;&下;的std ::获得&LT; 1 GT;(PR)LT;&LT; &gt;中;
}
};
}
}
print_log_value
是一个模板,所以如果你不声明像对℃的模板值; T,U&GT;
,你需要写类似:
模板&LT;&GT;
结构print_log_value&LT;&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屋!