Boost C++ Libraries: Ticket #12321: save for boost::optional doesn't call save_construct_data but load calls load_construct_data https://svn.boost.org/trac10/ticket/12321 <p> $ cat main.cpp </p> <pre class="wiki">#include &lt;stdexcept&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;boost/optional.hpp&gt; #include &lt;boost/archive/text_oarchive.hpp&gt; #include &lt;boost/archive/text_iarchive.hpp&gt; #include &lt;boost/serialization/optional.hpp&gt; struct Foo { Foo(int aBar) : mBar(aBar) { if (mBar &gt; 10) { throw std::logic_error("too big bar"); } } int bar() const { return mBar; } private: int mBar; }; namespace boost { namespace serialization { template&lt;class Archive&gt; inline void serialize(Archive &amp; ar, Foo&amp; foo, const unsigned int /*version*/) { std::cout &lt;&lt; __FUNCTION__ &lt;&lt; " called" &lt;&lt; std::endl; } template&lt;class Archive&gt; inline void save_construct_data(Archive &amp; ar, const Foo* foo, const unsigned int /*version*/) { std::cout &lt;&lt; __FUNCTION__ &lt;&lt; " called" &lt;&lt; std::endl; ar &lt;&lt; foo-&gt;bar(); } template&lt;class Archive&gt; inline void load_construct_data(Archive &amp; ar, Foo* foo, const unsigned int /*version*/) { std::cout &lt;&lt; __FUNCTION__ &lt;&lt; " called" &lt;&lt; std::endl; int bar; ar &gt;&gt; bar; ::new(foo) Foo(bar); } } // serialization } // boost int main() { boost::optional&lt;Foo&gt; oldFoo = Foo(10); std::ostringstream outStream; boost::archive::text_oarchive outArchive(outStream); outArchive &amp; oldFoo; boost::optional&lt;Foo&gt; newFoo; std::istringstream inStream(outStream.str()); boost::archive::text_iarchive inArchive(inStream); inArchive &amp; newFoo; } </pre><p> $ clang++ -I include -L lib -l boost_serialization --std=c++11 main.cpp </p> <p> $ LD_LIBRARY_PATH=lib ./a.out serialize called load_construct_data called terminate called after throwing an instance of 'boost::archive::archive_exception' </p> <blockquote> <p> what(): input stream error </p> </blockquote> <p> Aborted (core dumped) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12321 Trac 1.4.3 anonymous Mon, 03 Oct 2016 00:03:43 GMT <link>https://svn.boost.org/trac10/ticket/12321#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12321#comment:1</guid> <description> <p> I unfortunately ran into the same issue. When only serializing pointers the issue can be work arounded. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Tue, 01 Nov 2016 22:50:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12321#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12321#comment:2</guid> <description> <p> OK - this is tricky problem. I'll take a look at it. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Fri, 04 Nov 2016 20:08:43 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12321#comment:3 https://svn.boost.org/trac10/ticket/12321#comment:3 <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">fixed</span> </li> </ul> <p> I believe I have fixed this in a definitive way. I've checked it in to the develop branch. This is too late for boost 1.63 but will come out in next release </p> Ticket Robert Ramey Sun, 06 Nov 2016 21:38:02 GMT <link>https://svn.boost.org/trac10/ticket/12321#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12321#comment:4</guid> <description> <p> I had fixed your example and checked it in. But upon doing more testing I concluded that my "fix" would just break something else. Spending a lot more time on this convinced me that the root cause of the problem is the serialization of a type which out a default constructor. The library as currently constituted really can't do that - this is not due to the usage of "optional" it's a general limitation. Maybe if I made the library again I might address this limitation. Or maybe I tried originally but just accepted the limitation (without documenting it). So I'm going to make a few changes. </p> <p> a) fix it (again!) so that the test works. This will make your case fail </p> <p> b) update the documentation to clarify this point. There is a workaround. If a class has no default constructor it CAN be serialized through a pointer while overloading save/load construct data. </p> <p> c) use the boost type trait "has_default_constructor" to trap violations of tis rule and point to an explanation. </p> <p> Sorry you'll have to work around this. </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> </channel> </rss>