[Boost-bugs] [Boost C++ Libraries] #9990: boost deserialization of std::map does not work correctly

Subject: [Boost-bugs] [Boost C++ Libraries] #9990: boost deserialization of std::map does not work correctly
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-05-02 03:44:22


#9990: boost deserialization of std::map does not work correctly
------------------------------+---------------------------
 Reporter: geeksia1@… | Owner: ramey
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: serialization
  Version: Boost 1.54.0 | Severity: Showstopper
 Keywords: |
------------------------------+---------------------------
 I'm trying to compare two maps after serialization and deserialization of
 struct type. It gives me error -- "error in "MyExample": check
 e1_i1->second == e2_i1->second failed". I'm not able to figure out what
 could be wrong in below code :

 #include <boost/archive/text_oarchive.hpp>
 #include <boost/archive/text_iarchive.hpp>
 #include <boost/serialization/map.hpp>
 #include <boost/serialization/shared_ptr.hpp>

 struct A
 {
 public:
    std::string oldname;
    std::string newname;

    friend class boost::serialization::access;
       template<class Archive>
       void serialize(Archive &ar, const unsigned int version)
       {
          ar & oldname;
          ar & newname;
       }
 };

 struct Example
 {
 public:
    bool check;
    std::map<std::string,boost::shared_ptr<A>> Atype;

    private:
       friend class boost::serialization::access;
       template<class Archive>
       void serialize(Archive &ar, const unsigned int version)
       {
          ar & check;
          ar & Atype;
       }
 };

 void set_data(boost::shared_ptr<A> e)
 {
     e->oldname="a";
     e->newname="b";
 }

 void set_data(Example *e)
 {
    e->check=false;

    // to initialize e->Atype
    boost::shared_ptr<A> a1 (new A());
    set_data(a1);
    e->Atype.insert(std::make_pair("a",a1));
 }

 void compare_data(std::map<std::string,boost::shared_ptr<A>>
 e1,std::map<std::string,boost::shared_ptr<A>> e2)
 {
    // because it is of type map, it may be not in proper order
    typedef std::map<std::string,boost::shared_ptr<A>>::const_iterator i1;

    i1 e1_i1= e1.begin();
    i1 e2_i1 =e2.begin();

    while ( e1_i1 != e1.end() && e2_i1 != e2.end())
    {
       BOOST_CHECK( e1_i1->first == e2_i1->first);

       const std::string &key = e1_i1->first;
       e2_i1 =e2.find(key);
       BOOST_CHECK(e1_i1->second == e2_i1->second);

       e1_i1++;
       e2_i1++;
     }
 }

 void compare_data(Example *e1,Example *e2)
 {
    BOOST_CHECK(e1->check == e2->check);

    // need to compare e1->Atype with e2->Atype
    compare_data(e1->Atype,e2->Atype);
 }

 BOOST_AUTO_TEST_CASE(MyExample)
 {
    boost::archive::text_oarchive ao(std::cout);

    Example c;
    set_data(&c);

    const Example & oc=c;
    ao << oc;

    std::stringstream ss;
    boost::archive::text_oarchive oa(ss);
    oa << oc;

    boost::archive::text_iarchive ia(ss);

    Example d;
    ia >> d;

    compare_data(&c,&d);

 }

 different methods for initializing the map which i tried:

 //e->Atype=map_list_of ("a",a1); //
 e->Atype.insert(std::make_pair("a",a1)); //
 e->Atype.insert(std::map>::value_type("a",a1)); //e->Atype["a"]=a1;

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