[Boost-bugs] [Boost C++ Libraries] #11936: BOOST_TEST() misbehaves with boost::optional under C++11

Subject: [Boost-bugs] [Boost C++ Libraries] #11936: BOOST_TEST() misbehaves with boost::optional under C++11
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-01-23 14:42:15


#11936: BOOST_TEST() misbehaves with boost::optional under C++11
---------------------------------------+---------------------------
 Reporter: Tony Lewis <tonyelewis@…> | Owner: matias
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: Documentation
  Version: Boost 1.60.0 | Severity: Problem
 Keywords: BOOST_TEST,optional,c++11 |
---------------------------------------+---------------------------
 The following code shows BOOST_TEST() unexpectedly failing (contrary to
 BOOST_CHECK_EQUAL() passing) when compiled under C++11.

 {{{
 #!cpp
 #ifndef BOOST_TEST_DYN_LINK
 #define BOOST_TEST_DYN_LINK
 #endif
 #include <boost/test/unit_test.hpp>

 #include <boost/optional/optional.hpp>
 #include <boost/optional/optional_io.hpp>

 #include <iostream>

 class my_class {
 private:
         // Single data member
         boost::optional<int> num;

 public:
         // Ctor
         my_class(const boost::optional<int> &arg_num) : num ( arg_num ) {}

         // Getter
         const boost::optional<int> & get_num() const {
                 return num;
         }

         // Setter
         const my_class & set_num(const boost::optional<int> &arg_num) {
                 num = arg_num;
                 return *this;
         }
 };

 // Simple stream insertion operator
 std::ostream & operator<<(std::ostream &os, const my_class &my_obj) {
         os << "my_class[ " << my_obj.get_num() << " ]";
         return os;
 }

 // Simple equality predicate operator
 bool operator==(const my_class &my_obj_a, const my_class &my_obj_b) {
         return ( my_obj_a.get_num() == my_obj_b.get_num() );
 }

 BOOST_AUTO_TEST_CASE(my_test) {
         const my_class correct_answer( 5 );

         // These two work as expected
         BOOST_CHECK_EQUAL( my_class( boost::none ).set_num( 5 ),
 correct_answer );
         BOOST_CHECK_EQUAL( correct_answer,
 my_class( boost::none ).set_num( 5 ) );

         // These two fail, evaluating the non-trivial side as "my_class[
 -- ]"
         BOOST_TEST ( my_class( boost::none ).set_num( 5 ) ==
 correct_answer );
         BOOST_TEST ( correct_answer ==
 my_class( boost::none ).set_num( 5 ) );
 }

 bool init_function() { return true; }
 int main( int argc, char* argv[] ) {
     return ::boost::unit_test::unit_test_main( &init_function, argc, argv
 );
 }

 }}}

 I'm compiling and running with:

 {{{
 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
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11936>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:19 UTC