Boost C++ Libraries: Ticket #11936: BOOST_TEST() misbehaves with boost::optional under C++11 https://svn.boost.org/trac10/ticket/11936 <p> The following code shows BOOST_TEST() unexpectedly failing (contrary to BOOST_CHECK_EQUAL() passing) when compiled under C++11. </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#ifndef BOOST_TEST_DYN_LINK</span> <span class="cp">#define BOOST_TEST_DYN_LINK</span> <span class="cp">#endif</span> <span class="cp">#include</span> <span class="cpf">&lt;boost/test/unit_test.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/optional/optional.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/optional/optional_io.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="k">class</span> <span class="nc">my_class</span> <span class="p">{</span> <span class="k">private</span><span class="o">:</span> <span class="c1">// Single data member</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">num</span><span class="p">;</span> <span class="k">public</span><span class="o">:</span> <span class="c1">// Ctor</span> <span class="n">my_class</span><span class="p">(</span><span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">arg_num</span><span class="p">)</span> <span class="o">:</span> <span class="n">num</span> <span class="p">(</span> <span class="n">arg_num</span> <span class="p">)</span> <span class="p">{}</span> <span class="c1">// Getter</span> <span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="o">&amp;</span> <span class="n">get_num</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">num</span><span class="p">;</span> <span class="p">}</span> <span class="c1">// Setter</span> <span class="k">const</span> <span class="n">my_class</span> <span class="o">&amp;</span> <span class="n">set_num</span><span class="p">(</span><span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">optional</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="o">&amp;</span><span class="n">arg_num</span><span class="p">)</span> <span class="p">{</span> <span class="n">num</span> <span class="o">=</span> <span class="n">arg_num</span><span class="p">;</span> <span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="p">;</span> <span class="p">}</span> <span class="p">};</span> <span class="c1">// Simple stream insertion operator</span> <span class="n">std</span><span class="o">::</span><span class="n">ostream</span> <span class="o">&amp;</span> <span class="k">operator</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">ostream</span> <span class="o">&amp;</span><span class="n">os</span><span class="p">,</span> <span class="k">const</span> <span class="n">my_class</span> <span class="o">&amp;</span><span class="n">my_obj</span><span class="p">)</span> <span class="p">{</span> <span class="n">os</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;my_class[ &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">my_obj</span><span class="p">.</span><span class="n">get_num</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; ]&quot;</span><span class="p">;</span> <span class="k">return</span> <span class="n">os</span><span class="p">;</span> <span class="p">}</span> <span class="c1">// Simple equality predicate operator</span> <span class="kt">bool</span> <span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="k">const</span> <span class="n">my_class</span> <span class="o">&amp;</span><span class="n">my_obj_a</span><span class="p">,</span> <span class="k">const</span> <span class="n">my_class</span> <span class="o">&amp;</span><span class="n">my_obj_b</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="p">(</span> <span class="n">my_obj_a</span><span class="p">.</span><span class="n">get_num</span><span class="p">()</span> <span class="o">==</span> <span class="n">my_obj_b</span><span class="p">.</span><span class="n">get_num</span><span class="p">()</span> <span class="p">);</span> <span class="p">}</span> <span class="n">BOOST_AUTO_TEST_CASE</span><span class="p">(</span><span class="n">my_test</span><span class="p">)</span> <span class="p">{</span> <span class="k">const</span> <span class="n">my_class</span> <span class="n">correct_answer</span><span class="p">(</span> <span class="mi">5</span> <span class="p">);</span> <span class="c1">// These two work as expected</span> <span class="n">BOOST_CHECK_EQUAL</span><span class="p">(</span> <span class="n">my_class</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">none</span> <span class="p">).</span><span class="n">set_num</span><span class="p">(</span> <span class="mi">5</span> <span class="p">),</span> <span class="n">correct_answer</span> <span class="p">);</span> <span class="n">BOOST_CHECK_EQUAL</span><span class="p">(</span> <span class="n">correct_answer</span><span class="p">,</span> <span class="n">my_class</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">none</span> <span class="p">).</span><span class="n">set_num</span><span class="p">(</span> <span class="mi">5</span> <span class="p">)</span> <span class="p">);</span> <span class="c1">// These two fail, evaluating the non-trivial side as &quot;my_class[ -- ]&quot;</span> <span class="n">BOOST_TEST</span> <span class="p">(</span> <span class="n">my_class</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">none</span> <span class="p">).</span><span class="n">set_num</span><span class="p">(</span> <span class="mi">5</span> <span class="p">)</span> <span class="o">==</span> <span class="n">correct_answer</span> <span class="p">);</span> <span class="n">BOOST_TEST</span> <span class="p">(</span> <span class="n">correct_answer</span> <span class="o">==</span> <span class="n">my_class</span><span class="p">(</span> <span class="n">boost</span><span class="o">::</span><span class="n">none</span> <span class="p">).</span><span class="n">set_num</span><span class="p">(</span> <span class="mi">5</span> <span class="p">)</span> <span class="p">);</span> <span class="p">}</span> <span class="kt">bool</span> <span class="n">init_function</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="nb">true</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">(</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[]</span> <span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="o">::</span><span class="n">boost</span><span class="o">::</span><span class="n">unit_test</span><span class="o">::</span><span class="n">unit_test_main</span><span class="p">(</span> <span class="o">&amp;</span><span class="n">init_function</span><span class="p">,</span> <span class="n">argc</span><span class="p">,</span> <span class="n">argv</span> <span class="p">);</span> <span class="p">}</span> </pre></div></div><p> I'm compiling and running with: </p> <pre class="wiki">setenv BOOST_ROOT /opt/boost_1_60_0_gcc_build g++ -std=c++11 -isystem $BOOST_ROOT/include test_test_bug.cpp -L$BOOST_ROOT/lib -lboost_unit_test_framework-mt setenv LD_LIBRARY_PATH $BOOST_ROOT/lib ./a.out -l all </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11936 Trac 1.4.3 Michel Morin Fri, 26 Aug 2016 13:43:59 GMT owner, component changed https://svn.boost.org/trac10/ticket/11936#comment:1 https://svn.boost.org/trac10/ticket/11936#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Matias Capeletto</span> to <span class="trac-author">Gennadiy Rozental</span> </li> <li><strong>component</strong> <span class="trac-field-old">Documentation</span> → <span class="trac-field-new">test</span> </li> </ul> Ticket Raffi Enficiaud Sun, 04 Sep 2016 20:16:13 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11936#comment:2 https://svn.boost.org/trac10/ticket/11936#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">duplicate</span> </li> </ul> <p> Duplicates <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/12001" title="#12001: Bugs: Under C++11, BOOST_TEST() wrong on boost::optional + ... (closed: obsolete)">#12001</a> </p> Ticket Tony Lewis <tonyelewis@…> Tue, 13 Sep 2016 20:58:16 GMT <link>https://svn.boost.org/trac10/ticket/11936#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11936#comment:3</guid> <description> <p> Thanks for your work that has led this to get fixed. </p> </description> <category>Ticket</category> </item> </channel> </rss>