Opened 13 years ago

Closed 7 years ago

#2984 closed Bugs (invalid)

Cannot serialize protected and private base classes

Reported by: Eric Moyer <eric_moyer@…> 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)

boost_serialize_protected_bug_minimal.cc (1.4 KB ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
Minimal code to reproduce the problem
boost_serialize_protected_bug_minimal.compiler.output.txt (2.6 KB ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
Compiler output when I try to compile the minimal code
boost_serialize_protected_bug.cc (2.1 KB ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
boost_serialize_protected_bug.2.cc (2.1 KB ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
Minimal code to reproduce the problem + some macros to test serializing through base pointer and public vs. private vs. protected access
Makefile (690 bytes ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
Makefile I use to build the bug-reproducing code
boost_serialize_protected_bug_minimal_2.cc (1.0 KB ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
I removed one of the classes -- making the minimal even smaller
Makefile.2 (978 bytes ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
Updated makefile including _minimal_2
boost_serialize_protected_bug_minimal_2.compiler.output.txt (2.6 KB ) - added by Eric Moyer <eric_moyer@…> 13 years ago.
Compiler output when I try to compile the smaller minimal code
test_private_base.cpp (2.1 KB ) - added by Robert Ramey 7 years ago.
serialization of private base class
test_private_base2.cpp (2.9 KB ) - added by Robert Ramey 7 years ago.
cannot serialize through a private base class pointer

Download all attachments as: .zip

Change History (15)

by Eric Moyer <eric_moyer@…>, 13 years ago

Minimal code to reproduce the problem

by Eric Moyer <eric_moyer@…>, 13 years ago

Compiler output when I try to compile the minimal code

by Eric Moyer <eric_moyer@…>, 13 years ago

by Eric Moyer <eric_moyer@…>, 13 years ago

Minimal code to reproduce the problem + some macros to test serializing through base pointer and public vs. private vs. protected access

by Eric Moyer <eric_moyer@…>, 13 years ago

Attachment: Makefile added

Makefile I use to build the bug-reproducing code

by Eric Moyer <eric_moyer@…>, 13 years ago

I removed one of the classes -- making the minimal even smaller

by Eric Moyer <eric_moyer@…>, 13 years ago

Attachment: Makefile.2 added

Updated makefile including _minimal_2

by Eric Moyer <eric_moyer@…>, 13 years ago

Compiler output when I try to compile the smaller minimal code

comment:1 by Robert Ramey, 13 years ago

Resolution: fixed
Status: newclosed

Fixed in trunk. Should be included with 1.40

comment:2 by robtoth@…, 10 years ago

Resolution: fixed
Status: closedreopened

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".

in reply to:  2 comment:3 by Robert Ramey, 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 robtoth@…, 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 Robert Ramey, 7 years ago

Attachment: test_private_base.cpp added

serialization of private base class

by Robert Ramey, 7 years ago

Attachment: test_private_base2.cpp added

cannot serialize through a private base class pointer

comment:5 by Robert Ramey, 7 years ago

Resolution: invalid
Status: reopenedclosed

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.

Note: See TracTickets for help on using tickets.