[Boost-bugs] [Boost C++ Libraries] #12321: save for boost::optional doesn't call save_construct_data but load calls load_construct_data

Subject: [Boost-bugs] [Boost C++ Libraries] #12321: save for boost::optional doesn't call save_construct_data but load calls load_construct_data
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-07-10 14:25:55


#12321: save for boost::optional doesn't call save_construct_data but load calls
load_construct_data
-----------------------------------------------+---------------------------
 Reporter: Oleg Burchakov <burchakov.oleg@…> | Owner: ramey
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: serialization
  Version: Boost 1.61.0 | Severity: Problem
 Keywords: |
-----------------------------------------------+---------------------------
 $ cat main.cpp

 {{{
 #include <stdexcept>
 #include <iostream>
 #include <sstream>

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

 struct Foo
 {
     Foo(int aBar) :
         mBar(aBar)
     {
         if (mBar > 10)
         {
             throw std::logic_error("too big bar");
         }
     }
     int bar() const
     {
         return mBar;
     }
 private:
     int mBar;
 };


 namespace boost {
 namespace serialization {

 template<class Archive>
 inline void serialize(Archive & ar, Foo& foo, const unsigned int
 /*version*/)
 {
     std::cout << __FUNCTION__ << " called" << std::endl;
 }

 template<class Archive>
 inline void save_construct_data(Archive & ar, const Foo* foo, const
 unsigned int /*version*/)
 {
     std::cout << __FUNCTION__ << " called" << std::endl;
     ar << foo->bar();
 }


 template<class Archive>
 inline void load_construct_data(Archive & ar, Foo* foo, const unsigned int
 /*version*/)
 {
     std::cout << __FUNCTION__ << " called" << std::endl;
     int bar;
     ar >> bar;
     ::new(foo) Foo(bar);
 }

 } // serialization
 } // boost


 int main()
 {
     boost::optional<Foo> oldFoo = Foo(10);
     std::ostringstream outStream;
     boost::archive::text_oarchive outArchive(outStream);
     outArchive & oldFoo;

     boost::optional<Foo> newFoo;
     std::istringstream inStream(outStream.str());
     boost::archive::text_iarchive inArchive(inStream);
     inArchive & newFoo;
 }
 }}}

 $ clang++ -I include -L lib -l boost_serialization --std=c++11 main.cpp

 $ LD_LIBRARY_PATH=lib ./a.out
 serialize called
 load_construct_data called
 terminate called after throwing an instance of
 'boost::archive::archive_exception'
   what(): input stream error
 Aborted (core dumped)

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