Boost C++ Libraries: Ticket #11342: Vector serialization with non-default-constructible element type fails to compile https://svn.boost.org/trac10/ticket/11342 <p> The following sample compiles in Boost 1.57.0 but doesn't in Boost 1.58.0: </p> <pre class="wiki">#include &lt;boost/serialization/nvp.hpp&gt; #include &lt;boost/archive/xml_iarchive.hpp&gt; #include &lt;boost/archive/xml_oarchive.hpp&gt; class Foo{ friend class boost::serialization::access; template&lt;class Archive&gt; void serialize(Archive &amp; ar, const unsigned int) { ar &amp; BOOST_SERIALIZATION_NVP(i); } int i; Foo():i(0){} public: Foo(int k):i(k){} }; int main(int argc, char *argv[]) { std::vector&lt; Foo&gt; f; f.push_back(Foo(12)); std::ofstream os("path"); boost::archive::xml_oarchive oa(os); oa &lt;&lt; boost::serialization::make_nvp("f", f); os.close(); std::vector&lt;Foo&gt; g; std::ifstream is("path"); boost::archive::xml_iarchive ia(is); ia &gt;&gt; boost::serialization::make_nvp("f", g); } </pre><p> The problem is a new piece of code that tries to switch on <code>is_default_constructible&lt;T&gt;</code> <strong>*at compile time</strong>*. That can't work: (from serialization/vector.hpp in the unoptimized version of load(...)): </p> <pre class="wiki">if(detail::is_default_constructible&lt;U&gt;()){ t.resize(count); // ... snip ... } else{ t.reserve(count); // ... snip ... } </pre><p> Instead of having the branches in the same flow code, it should be dispatched so that only the applicable branch is instantiated. I tested with this simplistic approach: </p> <pre class="wiki">namespace sehe_bugfix { template&lt;class Archive, class U, class Allocator&gt; inline void load_elements( Archive &amp; ar, std::vector&lt;U, Allocator&gt; &amp;t, const unsigned int /* file_version */, collection_size_type count, mpl::true_ ){ const boost::archive::library_version_type library_version( ar.get_library_version() ); item_version_type item_version(0); if(boost::archive::library_version_type(3) &lt; library_version){ ar &gt;&gt; BOOST_SERIALIZATION_NVP(item_version); } t.resize(count); typename std::vector&lt;U, Allocator&gt;::iterator hint; hint = t.begin(); while(count-- &gt; 0){ ar &gt;&gt; boost::serialization::make_nvp("item", *hint++); } } template&lt;class Archive, class U, class Allocator&gt; inline void load_elements( Archive &amp; ar, std::vector&lt;U, Allocator&gt; &amp;t, const unsigned int /* file_version */, collection_size_type count, mpl::false_ ){ const boost::archive::library_version_type library_version( ar.get_library_version() ); item_version_type item_version(0); if(boost::archive::library_version_type(3) &lt; library_version){ ar &gt;&gt; BOOST_SERIALIZATION_NVP(item_version); } t.reserve(count); while(count-- &gt; 0){ detail::stack_construct&lt;Archive, U&gt; u(ar, item_version); ar &gt;&gt; boost::serialization::make_nvp("item", u.reference()); t.push_back(u.reference()); ar.reset_object_address(&amp; t.back() , &amp; u.reference()); } } } template&lt;class Archive, class U, class Allocator&gt; inline void load( Archive &amp; ar, std::vector&lt;U, Allocator&gt; &amp;t, const unsigned int file_version, mpl::false_ ){ const boost::archive::library_version_type library_version( ar.get_library_version() ); // retrieve number of elements item_version_type item_version(0); collection_size_type count; ar &gt;&gt; BOOST_SERIALIZATION_NVP(count); sehe_bugfix::load_elements(ar, t, file_version, count, detail::is_default_constructible&lt;U&gt;()); } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11342 Trac 1.4.3 bugs@… Mon, 25 May 2015 23:53:08 GMT <link>https://svn.boost.org/trac10/ticket/11342#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11342#comment:1</guid> <description> <p> PS. This was in reference to this SO post: <a class="ext-link" href="http://stackoverflow.com/a/30437359/85371"><span class="icon">​</span>http://stackoverflow.com/a/30437359/85371</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Tue, 26 May 2015 04:20:36 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/11342#comment:2 https://svn.boost.org/trac10/ticket/11342#comment:2 <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> The fix for this has been checked into the develop and master branch and will appear in the next release. If you can't wait for that, you can clone/download the latest versions from github. </p> Ticket