Boost C++ Libraries: Ticket #2984: Cannot serialize protected and private base classes https://svn.boost.org/trac10/ticket/2984 <p> boost::serialization will not compile code that serializes a class that has non-public base classes. It complains about an inaccessible base -- despite the friend definition for serialization::access. </p> <p> There is a workaround for private base classes, since those can be emulated by private member variables, but I can't think of any way to work around the protected inheritance problem. </p> <p> I am using g++ 4.3.3 Running on Ubuntu 9.04 </p> <p> I have attached two source files that cause the problem, one minimal and one slightly less minimal (with some preprocessor macros I used to trace down the problem). </p> <p> I have also included the compiler output for the minimal code and the Makefile I use to build them. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2984 Trac 1.4.3 Eric Moyer <eric_moyer@…> Sat, 02 May 2009 17:56:04 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_serialize_protected_bug_minimal.cc</span> </li> </ul> <p> Minimal code to reproduce the problem </p> Ticket Eric Moyer <eric_moyer@…> Sat, 02 May 2009 17:56:39 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_serialize_protected_bug_minimal.compiler.output.txt</span> </li> </ul> <p> Compiler output when I try to compile the minimal code </p> Ticket Eric Moyer <eric_moyer@…> Sat, 02 May 2009 17:56:59 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_serialize_protected_bug.cc</span> </li> </ul> Ticket Eric Moyer <eric_moyer@…> Sat, 02 May 2009 17:58:37 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_serialize_protected_bug.2.cc</span> </li> </ul> <p> Minimal code to reproduce the problem + some macros to test serializing through base pointer and public vs. private vs. protected access </p> Ticket Eric Moyer <eric_moyer@…> Sat, 02 May 2009 17:59:24 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">Makefile</span> </li> </ul> <p> Makefile I use to build the bug-reproducing code </p> Ticket Eric Moyer <eric_moyer@…> Sat, 02 May 2009 18:07:42 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_serialize_protected_bug_minimal_2.cc</span> </li> </ul> <p> I removed one of the classes -- making the minimal even smaller </p> Ticket Eric Moyer <eric_moyer@…> Sat, 02 May 2009 18:08:23 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">Makefile.2</span> </li> </ul> <p> Updated makefile including _minimal_2 </p> Ticket Eric Moyer <eric_moyer@…> Sat, 02 May 2009 18:08:56 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_serialize_protected_bug_minimal_2.compiler.output.txt</span> </li> </ul> <p> Compiler output when I try to compile the smaller minimal code </p> Ticket Robert Ramey Mon, 18 May 2009 16:04:43 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2984#comment:1 https://svn.boost.org/trac10/ticket/2984#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> Fixed in trunk. Should be included with 1.40 </p> Ticket robtoth@… Mon, 21 Jan 2013 21:53:14 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/2984#comment:2 https://svn.boost.org/trac10/ticket/2984#comment:2 <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">fixed</span> </li> </ul> <p> I just tried this on gcc 4.7 and the minimal example still doesn't compile. Was this ever fixed? Is there a workaround? The error is still "boost/serialization/void_cast.hpp:191: error: Base1 is an inaccessible base of Derived". </p> Ticket Robert Ramey Tue, 22 Jan 2013 05:38:58 GMT <link>https://svn.boost.org/trac10/ticket/2984#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2984#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2984#comment:2" title="Comment 2">robtoth@…</a>: </p> <p> I made this program compile by adding </p> <pre class="wiki"> friend class Derived; // make Derived a friend ... virtual void print() const { // add "const" here cout &lt;&lt; "Derived" &lt;&lt; endl; } ... const Derived d; // add "const" here </pre><p> The addition of "const" is pretty obvious. </p> <p> The addition of "friend" is not obvious and wouldn't seem necessary. On the other hand, I looked at the code and don't see an easy way to fix this. </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <author>robtoth@…</author> <pubDate>Thu, 24 Jan 2013 16:42:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2984#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2984#comment:4</guid> <description> <p> Sorry but I still can't compile this. The const is fine and I understand it, but the "friend" doesn't help. </p> <p> <em>boost_serialize_protected_bug_minimal_2.cc:</em> </p> <pre class="wiki">... class Base1{ friend class boost::serialization::access; friend class Derived; // Added this ... </pre><p> I still get the error: </p> <pre class="wiki">include/boost/serialization/smart_cast.hpp:204: error: 'Base1' is an inaccessible base of 'Derived' </pre><p> This is on gcc svn trunk (2013-01-22, version 4.8) and boost svn trunk (as of 2013-01-22, version 1.53.0). </p> <p> Did you do something different from what I did to make Derived serialize? </p> <p> I can attach the full error output if it would help. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Fri, 02 Oct 2015 00:34:38 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_private_base.cpp</span> </li> </ul> <p> serialization of private base class </p> Ticket Robert Ramey Fri, 02 Oct 2015 00:35:47 GMT attachment set https://svn.boost.org/trac10/ticket/2984 https://svn.boost.org/trac10/ticket/2984 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_private_base2.cpp</span> </li> </ul> <p> cannot serialize through a private base class pointer </p> Ticket Robert Ramey Fri, 02 Oct 2015 00:41:20 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2984#comment:5 https://svn.boost.org/trac10/ticket/2984#comment:5 <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">invalid</span> </li> </ul> <p> OK _ looked at this more carefully. </p> <p> There is more going on here than meets the eye. </p> <p> test_private_base.cpp demonstrates that one can serialize a private base class with no problem. </p> <p> test_private_base.cpp demonstrates the problem which occurs when one tries to serialized through a pointer to a private base class. The program fails to compile at line 90. Basically one cannot do this in C++. I've searched in vain for a way to do this but haven't found any. The BOOST_CLASS_EXPORT uses the same functionality to implement this facility so it has the same problem. </p> <p> Bottom line - serializatiion of private/protected base classes is fine - but one cannot serialize such classes through a base class pointer. </p> Ticket