Boost C++ Libraries: Ticket #11907: Why does BOOST_TEST() treat std::string as a collection? https://svn.boost.org/trac10/ticket/11907 <p> Captured from <a class="ext-link" href="http://lists.boost.org/boost-users/2015/12/85472.php"><span class="icon">​</span>http://lists.boost.org/boost-users/2015/12/85472.php</a> </p> <p> Why does BOOST_TEST() compare std::string values as collections instead of scalars (by default)? It's odd that the following produce different output, for example: </p> <pre class="wiki">BOOST_TEST(std::string("a") == "b"); BOOST_TEST(std::string("a") == std::string("b")); </pre><p> This will output: </p> <pre class="wiki">test.cc(6): error: in "test": check std::string("a") == "b" has failed [a != b] test.cc(7): error: in "test": check std::string("a") == std::string("b") has failed </pre><p> Note the lack of "[a != b]" on the second case. Also note that BOOST_CHECK_EQUAL() would emit as in the first form. This seems to be because std::string is_forward_iterable, and thus falls into the collection_comparison_op.hpp comparators instead of the scalar comparators. Is this intentional or an oversight? </p> <p> As a simple fix, adding a condition to the partial specialization of the collection_comparison_op.hpp comparators that specifically excludes std::string is enough to have std::string treated as scalars. For example, to handle std::string but not std::wstring with C++11 support: </p> <pre class="wiki">template&lt;typename Lhs,typename Rhs&gt; struct name&lt;Lhs,Rhs,typename boost::enable_if_c&lt; unit_test::is_forward_iterable&lt;Lhs&gt;::value &amp;&amp; unit_test::is_forward_iterable&lt;Rhs&gt;::value &amp;&amp; !(std::is_same&lt;typename std::decay&lt;Lhs&gt;::type, std::string&gt;::value || std::is_same&lt;typename std::decay&lt;Rhs&gt;::type, std::string&gt;::value) &gt;::type </pre><p> (The above is an example and not a complete change supporting std::wstring and C++98.) </p> <p> The response on the mailing list was that this was a bug, so here's a ticket to help track it :) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11907 Trac 1.4.3 Raffi Enficiaud Tue, 04 Oct 2016 06:52:21 GMT owner, status, milestone changed https://svn.boost.org/trac10/ticket/11907#comment:1 https://svn.boost.org/trac10/ticket/11907#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Gennadiy Rozental</span> to <span class="trac-author">Raffi Enficiaud</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.63.0</span> </li> </ul> Ticket Raffi Enficiaud Sun, 11 Dec 2016 18:54:36 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11907#comment:2 https://svn.boost.org/trac10/ticket/11907#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket