Boost C++ Libraries: Ticket #12876: BOOST_CHECK_EQUAL fail for vector<int> with operator<< for vector<T> https://svn.boost.org/trac10/ticket/12876 <p> #include &lt;boost/test/unit_test.hpp&gt; #include &lt;iostream&gt; #include &lt;ostream&gt; #include &lt;vector&gt; </p> <p> using namespace std; </p> <p> class <a class="missing wiki">PrintClass</a> { </p> <p> }; bool operator== (const <a class="missing wiki">PrintClass</a> &amp;l, const <a class="missing wiki">PrintClass</a> &amp;r) { </p> <blockquote> <p> std::cout &lt;&lt; "value compare" &lt;&lt; std::endl; return false; </p> </blockquote> <p> } </p> <p> template &lt;typename T&gt; std::ostream &amp; operator&lt;&lt; (std::ostream &amp; os, std::vector&lt;T&gt; const &amp;v) { </p> <blockquote> <p> os &lt;&lt; "has " &lt;&lt; &amp;v; return os; </p> </blockquote> <p> } </p> <p> template &lt;typename T&gt; bool operator== (const std::vector&lt;T&gt; &amp;l, const std::vector&lt;T&gt; &amp;r) { </p> <blockquote> <p> std::cout &lt;&lt; "const vector compare" &lt;&lt; std::endl; return false; </p> </blockquote> <p> } </p> <p> BOOST_AUTO_TEST_CASE(test_print_vector_int) { </p> <blockquote> <p> const std::vector&lt;int&gt; a<a class="report" href="https://svn.boost.org/trac10/report/1">{1}</a>; const std::vector&lt;int&gt; b<a class="report" href="https://svn.boost.org/trac10/report/2">{2}</a>; </p> </blockquote> <blockquote> <p> std::cout &lt;&lt; a &lt;&lt; std::endl; <em> work BOOST_CHECK_EQUAL(a, b); </em> don't work. <em> error: static assertion failed: Type has to implement operator&lt;&lt; to be printable </em></p> </blockquote> <blockquote> <p> <em>std::cout &lt;&lt; boost::has_left_shift&lt;std::ostream, vector&lt;int&gt;&gt;::value &lt;&lt; endl; </em></p> </blockquote> <blockquote> <p> <em> use user define class is ok std::vector&lt;<a class="missing wiki">PrintClass</a>&gt; c{<a class="missing wiki">PrintClass</a>()}; std::vector&lt;<a class="missing wiki">PrintClass</a>&gt; d{<a class="missing wiki">PrintClass</a>()}; std::cout &lt;&lt; c &lt;&lt; std::endl; </em> work BOOST_CHECK_EQUAL(c, d); <em> work </em></p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12876 Trac 1.4.3 401819174@… Wed, 01 Mar 2017 02:44:34 GMT <link>https://svn.boost.org/trac10/ticket/12876#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12876#comment:1</guid> <description> <p> BOOST_AUTO_TEST_CASE(test_print_vector_int) { </p> <blockquote> <p> const std::vector&lt;int&gt; a<a class="report" href="https://svn.boost.org/trac10/report/1">{1}</a>; </p> </blockquote> <blockquote> <p> const std::vector&lt;int&gt; b<a class="report" href="https://svn.boost.org/trac10/report/2">{2}</a>; </p> </blockquote> <blockquote> <p> std::cout &lt;&lt; a &lt;&lt; std::endl; /* work */ </p> </blockquote> <blockquote> <p> BOOST_CHECK_EQUAL(a, b); /* don't work. */ </p> </blockquote> <blockquote> <p> /* error: static assertion failed: Type has to implement operator&lt;&lt; to be printable */ </p> </blockquote> <blockquote> <p> /* std::cout &lt;&lt; boost::has_left_shift&lt;std::ostream, vector&lt;int&gt;&gt;::value &lt;&lt; endl;*/ </p> </blockquote> <blockquote> <p> /* use user define class is ok */ </p> </blockquote> <blockquote> <p> std::vector&lt;<a class="missing wiki">PrintClass</a>&gt; c{<a class="missing wiki">PrintClass</a>()}; std::vector&lt;<a class="missing wiki">PrintClass</a>&gt; d{<a class="missing wiki">PrintClass</a>()}; std::cout &lt;&lt; c &lt;&lt; std::endl; /* work */ BOOST_CHECK_EQUAL(c, d); /* work */ </p> </blockquote> <p> } </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Raffi Enficiaud</dc:creator> <pubDate>Wed, 01 Mar 2017 11:31:08 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12876#comment:2 https://svn.boost.org/trac10/ticket/12876#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> You have <a href="http://www.boost.org/doc/libs/1_63_0/libs/test/doc/html/boost_test/utf_reference/testing_tool_ref/assertion_boost_level_eq_collections.html">BOOST_CHECK_EQUAL_COLLECTIONS</a> or <a href="http://www.boost.org/doc/libs/1_63_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/collections.html">BOOST_TEST</a> that works on collections as well. </p> <p> If the underlying type is not printable, then you should consider using <a href="http://www.boost.org/doc/libs/1_63_0/libs/test/doc/html/boost_test/utf_reference/testout_reference/test_output_macro_disable_type.html">BOOST_TEST_DONT_PRINT_LOG_VALUE</a> </p> <p> Best, Raffi </p> Ticket 401819174@… Thu, 02 Mar 2017 03:10:21 GMT <link>https://svn.boost.org/trac10/ticket/12876#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12876#comment:3</guid> <description> <p> thank you </p> <blockquote> <p> the C++ function overload mechanism require all overload function in the same domain. vector/int, make search the function from std or boost::<strong>*(boost::has_left_shift). in the std has the define of operator&lt;&lt;, so in the global define of operator&lt;&lt; is not visiable。 </strong></p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>Raffi Enficiaud</dc:creator> <pubDate>Thu, 02 Mar 2017 08:07:11 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12876#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12876#comment:4</guid> <description> <p> I do not understand everything you are saying (please use the Wiki Markup), but here is what I think: </p> <pre class="wiki">const std::vector&lt;int&gt; a{1}; const std::vector&lt;int&gt; b{2}; BOOST_CHECK_EQUAL(a, b); /* does not work. */ </pre><p> This does not work because it requires <code>std::vector&lt;int&gt;</code> to be printable, which is usually never the case by default. However </p> <pre class="wiki">BOOST_CHECK_EQUAL_COLLECTIONS(a.begin(), a.end(), b.begin(), b.end()); </pre><p> works as expected, because the underlying <code>int</code> is already printable. </p> <p> Concerning </p> <pre class="wiki">std::vector&lt;PrintClass&gt; c{PrintClass()}; std::vector&lt;PrintClass&gt; d{PrintClass()}; BOOST_CHECK_EQUAL(c, d); </pre><p> In your example, this works because you have this template that is capture the prints to std::cout: </p> <pre class="wiki">template &lt;typename T&gt; std::ostream &amp; operator&lt;&lt; ( std::ostream &amp; os, std::vector&lt;T&gt; const &amp;v) </pre><p> Your bug is invalid. </p> </description> <category>Ticket</category> </item> </channel> </rss>