Boost C++ Libraries: Ticket #4370: [Serialization] map with private default constructor does not compile https://svn.boost.org/trac10/ticket/4370 <p> I believe that collections_load_impl.hpp is somewhat incorrect in its implementation of loading for maps. The creation of the value_type of the map on the stack results in a call to the default std::pair constructor which will fail if the keys or the values do not have a public default constructor. Note that this problem is specific to maps and does not show in sequential containers. </p> <p> In the case of nondefault constructible key/value the default constructor of the pair cannot be used. </p> <p> Problem appears on Ubuntu with gcc 4.4.3 and the default (for Ubuntu) boost version 1.40. I do not have a compiled copy of boost 1.43, but it seems that the code is still the same. There was also no mention of the issue as being fixed in the recent changes to the Serialization library since 1.40. </p> <p> A simple file to reproduce the problem is attached. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4370 Trac 1.4.3 anonymous Wed, 23 Jun 2010 23:47:49 GMT attachment set https://svn.boost.org/trac10/ticket/4370 https://svn.boost.org/trac10/ticket/4370 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test.cpp</span> </li> </ul> Ticket Robert Ramey Mon, 28 Jun 2010 16:42:39 GMT <link>https://svn.boost.org/trac10/ticket/4370#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4370#comment:1</guid> <description> <p> When I compile your test, I get a compilation error at the following statement </p> <p> std::map&lt;int,A&gt;::value_type m1(1,A(10)); </p> <p> This has nothing to do with the serialization library. I don't see how I could possibly fix this from within the serialization library. </p> <p> I believe your commented out fix is the correct one since the elements of A are in fact created by std::pair </p> <p> I'm labeling this "wont fix" only because there is not "cant fix" </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Mon, 28 Jun 2010 16:43:33 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4370#comment:2 https://svn.boost.org/trac10/ticket/4370#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">wontfix</span> </li> </ul> Ticket anonymous Wed, 30 Jun 2010 16:43:32 GMT <link>https://svn.boost.org/trac10/ticket/4370#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4370#comment:3</guid> <description> <p> Unfortunately I have to disagree and I would like to kindly ask that you take another look at this. Yes the compiler complains about std::pair construction, but not the one you referred to -- at least my compiler complains about the construction in /usr/include/boost/serialization/access.hpp:123 (please, take a look at the compiler output which is attached). I suggest that you comment out lines 60 and 65 (the ones with BOOST_SERIALIZATION_NVP(amap) ) and you will see that the line you referred to in your reply is fine because the constructor in line 54 is the one which takes the inputs as references; in fact the only solution I have been able to come up with is to modify collections_load_impl.hpp to use the constructor of std::pair which takes references as inputs as opposed to no inputs and calls the default constructors). </p> <p> It is possible that I am misunderstanding something and in such case I would appreciate if you help me clear up my confusion (even though you certainly do not have to). </p> <p> NK </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 30 Jun 2010 16:43:59 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4370 https://svn.boost.org/trac10/ticket/4370 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_g++.out</span> </li> </ul> Ticket anonymous Wed, 30 Jun 2010 16:44:51 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/4370#comment:4 https://svn.boost.org/trac10/ticket/4370#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">wontfix</span> </li> </ul> Ticket Robert Ramey Fri, 02 Jul 2010 20:15:47 GMT <link>https://svn.boost.org/trac10/ticket/4370#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4370#comment:5</guid> <description> <p> I've double checked. </p> <p> I do indeed get a compiler error on the following line </p> <blockquote> <p> std::map&lt;int,A&gt;::value_type m1(1,A(10)); </p> </blockquote> <p> which I'm not really understanding as you point out shouldn't be instantitiating the default constructor. In any case, that's an MSVC and/or standard library issue. </p> <p> I do see the code in collections_load_impl.hpp you refer to. </p> <p> So ... what do you want me to do? Do you have a patch to submit? </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 06 Jul 2010 23:29:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4370#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4370#comment:6</guid> <description> <p> I will try to put something together, but it will take me some time (as I have been using the default boost on Ubuntu) because I will need to compile my own local copy and familiarize myself with the inner workings of the serialization library. NK </p> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4370#comment:5" title="Comment 5">ramey</a>: </p> <blockquote class="citation"> <p> I've double checked. </p> <p> I do indeed get a compiler error on the following line </p> <blockquote> <p> std::map&lt;int,A&gt;::value_type m1(1,A(10)); </p> </blockquote> <p> which I'm not really understanding as you point out shouldn't be instantitiating the default constructor. In any case, that's an MSVC and/or standard library issue. </p> <p> I do see the code in collections_load_impl.hpp you refer to. </p> <p> So ... what do you want me to do? Do you have a patch to submit? </p> <p> Robert Ramey </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Wed, 04 Aug 2010 19:16:00 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4370#comment:7 https://svn.boost.org/trac10/ticket/4370#comment:7 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> I"m going to mark this as "can't fix" since I can't do anything with it. </p> <p> Robert Ramey </p> Ticket boost@… Fri, 17 Jun 2011 16:40:03 GMT <link>https://svn.boost.org/trac10/ticket/4370#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4370#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4370#comment:5" title="Comment 5">ramey</a>: </p> <blockquote class="citation"> <p> I do indeed get a compiler error on the following line </p> <blockquote> <p> std::map&lt;int,A&gt;::value_type m1(1,A(10)); </p> </blockquote> <p> which I'm not really understanding as you point out shouldn't be instantitiating the default constructor. In any case, that's an MSVC and/or standard library issue. </p> </blockquote> <p> Robert, we've also seen this. It is indeed MSVC getting confused, resulting in it thinking a default constructed pair must be required. </p> <p> However, it IS triggered by the attempt to serialize a std::pair where at least one of the arguments has a private default ctor. This can be seen by commenting out lines 60 and 65 (as they appear in trac) in the OP's test.cpp. </p> <blockquote class="citation"> <p> So ... what do you want me to do? Do you have a patch to submit? </p> </blockquote> <p> Because the key or value type of the std::pair will (typically) have been given friend access to boost::serialization::access, it is annoying that the pair trips up. However, the following appears to be a suitable patch (unless I've missed something): </p> <p> <em> Add to boost/serialization/utility.hpp: </em></p> <p> template &lt;typename Archive, typename First, typename Second&gt; void load_construct_data(Archive &amp; ar, std::pair&lt;First, Second&gt; * t, unsigned int const) { </p> <blockquote> <p> typedef BOOST_DEDUCED_TYPENAME remove_const&lt;First&gt;::type <a class="missing wiki">NonConstFirst</a>; <a class="missing wiki">NonConstFirst</a> &amp;first = const_cast&lt;<a class="missing wiki">NonConstFirst</a>&amp;&gt;(t-&gt;first); access::construct(&amp;first); access::construct(&amp;t-&gt;second); </p> </blockquote> <p> } </p> <p> <em></em><em></em><em></em><em>/ </em></p> <p> I.e. instead of invoking the default ctor of std::pair&lt;&gt;, route through boost::serialization::access to in-place construct the two values. </p> </description> <category>Ticket</category> </item> <item> <author>boost@…</author> <pubDate>Fri, 17 Jun 2011 16:42:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4370#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4370#comment:9</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4370#comment:8" title="Comment 8">boost@…</a>: </p> <p> Forgot formatting: </p> <pre class="wiki">template &lt;typename Archive, typename First, typename Second&gt; void load_construct_data(Archive &amp; ar, std::pair&lt;First, Second&gt; * t, unsigned int const) { typedef BOOST_DEDUCED_TYPENAME remove_const&lt;First&gt;::type NonConstFirst; NonConstFirst &amp;first = const_cast&lt;NonConstFirst&amp;&gt;(t-&gt;first); access::construct(&amp;first); access::construct(&amp;t-&gt;second); } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 14 Oct 2011 13:59:08 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4370#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4370#comment:10</guid> <description> <p> Same problem here. </p> <p> The culprit is with MSVC reporting an error at the template instantiation point, instead of pointing to the actual place where the default constructor of std::pair (and thus of the UDT without default constructor) is used. </p> </description> <category>Ticket</category> </item> </channel> </rss>