Boost C++ Libraries: Ticket #11343: load/save_construct_data not enough to serialize some containers of UDT https://svn.boost.org/trac10/ticket/11343 <p> In reference to this answer on SO <a class="ext-link" href="http://stackoverflow.com/a/30437785/85371"><span class="icon">​</span>http://stackoverflow.com/a/30437785/85371</a> </p> <p> (De)serializing a map with a non-default-constructible mapped type of <code>Foo</code> fails to compile, even if <code>save_construct_data</code> and <code>load_construct_data</code> have been overloaded for this type. </p> <blockquote class="citation"> <p> *Note:* Part of this seems related to the (version of the) standard library implementation in use (see the linked SO post) </p> </blockquote> <p> One can get around this by overloading for the <code>std::pair&lt;K const, Foo&gt;</code> type that is _actually_ the element type for e.g. <code>std::map&lt;K, Foo&gt;</code>, e.g. for the specific <code>Foo</code> sample: </p> <pre class="wiki"> template &lt;class Archive, typename K&gt; inline friend void save_construct_data(Archive&amp; ar, std::pair&lt;K, Foo&gt; const* v, const unsigned int) { ar &amp; boost::serialization::make_nvp("first", v-&gt;first); ar &amp; boost::serialization::make_nvp("second", v-&gt;second.i); } template &lt;class Archive, typename K&gt; inline friend void load_construct_data(Archive&amp; ar, std::pair&lt;K, Foo&gt;* v, const unsigned int) { typename std::remove_cv&lt;K&gt;::type first; ar &amp; boost::serialization::make_nvp("first", first); int tmp; ar &amp; boost::serialization::make_nvp("second", tmp); new(v) std::pair&lt;K, Foo&gt;(first, tmp); } </pre><p> However, this is not transparant (people have to provide different <code>load/save_construct_data</code> overloads depending on which containers are going to contain their type) and it's repetitive work, which is also hard to get right. </p> <hr /> <p> A better, general, solution would be to do the load/save_construct_data trick generically for non-default-constructible types in the boost::serialization namespace. That way, people won't have to "know" about the std::pair&lt;&gt; implementation detail. They could just implement load/save_construct_data for their own user-types and it would <a class="missing wiki">JustWork</a>™ whether they put it in a vector or a map. </p> <p> Implementing that generically is less-than-trivial though, and might interfere with some other machineries internal to the Boost Serialization framework. </p> <p> I'll prefer to get some help of the Boost Serialization maintainers to do that in a reliable way. So, it seems I'll be submitting two tickets today. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11343 Trac 1.4.3 Robert Ramey Tue, 26 May 2015 04:21:57 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11343#comment:1 https://svn.boost.org/trac10/ticket/11343#comment:1 <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