[Boost-bugs] [Boost C++ Libraries] #12001: Under C++11, BOOST_TEST() wrong on boost::optional + named-parameter-idiom example

Subject: [Boost-bugs] [Boost C++ Libraries] #12001: Under C++11, BOOST_TEST() wrong on boost::optional + named-parameter-idiom example
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-02-18 22:10:43


#12001: Under C++11, BOOST_TEST() wrong on boost::optional + named-parameter-idiom
example
-------------------------------------------------+-------------------------
 Reporter: Tony Lewis <tonyelewis@…> | Owner: rogeeff
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: test
  Version: Boost 1.60.0 | Severity: Problem
 Keywords: |
  test,optional,BOOST_TEST,BOOST_CHECK_EQUAL,c++11,named|
  parameter idiom |
-------------------------------------------------+-------------------------
 The example below shows the `BOOST_TEST()` assertions failing where they
 should pass, as the equivalent `BOOST_CHECK_EQUAL()` assertions do. It
 looks as though `BOOST_TEST()` is preventing the named-parameter-idiom
 setter from changing the `optional` value from `boost::none`.

 Importantly, this code works if compiled without `-std=c++11`.

 Compile commands:

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

 Code:

 {{{
 #!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:
         boost::optional<int> num;

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

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

         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
 );
 }

 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/12001>
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