Opened 13 years ago
Closed 7 years ago
#2984 closed Bugs (invalid)
Cannot serialize protected and private base classes
Reported by: | Owned by: | Robert Ramey | |
---|---|---|---|
Milestone: | Component: | serialization | |
Version: | Boost 1.37.0 | Severity: | Problem |
Keywords: | Cc: |
Description
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.
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.
I am using g++ 4.3.3 Running on Ubuntu 9.04
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).
I have also included the compiler output for the minimal code and the Makefile I use to build them.
Attachments (10)
Change History (15)
by , 13 years ago
Attachment: | boost_serialize_protected_bug_minimal.cc added |
---|
by , 13 years ago
Attachment: | boost_serialize_protected_bug_minimal.compiler.output.txt added |
---|
Compiler output when I try to compile the minimal code
by , 13 years ago
Attachment: | boost_serialize_protected_bug.cc added |
---|
by , 13 years ago
Attachment: | boost_serialize_protected_bug.2.cc added |
---|
Minimal code to reproduce the problem + some macros to test serializing through base pointer and public vs. private vs. protected access
by , 13 years ago
Attachment: | boost_serialize_protected_bug_minimal_2.cc added |
---|
I removed one of the classes -- making the minimal even smaller
by , 13 years ago
Attachment: | boost_serialize_protected_bug_minimal_2.compiler.output.txt added |
---|
Compiler output when I try to compile the smaller minimal code
comment:1 by , 13 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Fixed in trunk. Should be included with 1.40
follow-up: 3 comment:2 by , 10 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
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".
comment:3 by , 10 years ago
Replying to robtoth@…:
I made this program compile by adding
friend class Derived; // make Derived a friend ... virtual void print() const { // add "const" here cout << "Derived" << endl; } ... const Derived d; // add "const" here
The addition of "const" is pretty obvious.
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.
Robert Ramey
comment:4 by , 10 years ago
Sorry but I still can't compile this. The const is fine and I understand it, but the "friend" doesn't help.
boost_serialize_protected_bug_minimal_2.cc:
... class Base1{ friend class boost::serialization::access; friend class Derived; // Added this ...
I still get the error:
include/boost/serialization/smart_cast.hpp:204: error: 'Base1' is an inaccessible base of 'Derived'
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).
Did you do something different from what I did to make Derived serialize?
I can attach the full error output if it would help.
by , 7 years ago
Attachment: | test_private_base2.cpp added |
---|
cannot serialize through a private base class pointer
comment:5 by , 7 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
OK _ looked at this more carefully.
There is more going on here than meets the eye.
test_private_base.cpp demonstrates that one can serialize a private base class with no problem.
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.
Bottom line - serializatiion of private/protected base classes is fine - but one cannot serialize such classes through a base class pointer.
Minimal code to reproduce the problem