Opened 12 years ago

Closed 7 years ago

#4681 closed Feature Requests (fixed)

can't serialize protected derived class

Reported by: mark.r.buda@… Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost 1.40.0 Severity: Problem
Keywords: Cc:

Description

The attached code won't compile because wrapper::derived is a protected member of the wrapper class.

I have a workaround, which will be to write my own macros to achieve the same effect as BOOST_CLASS_EXPORT_GUID in this case, but it'd be nice if the library worked out of the box in this case.

Attachments (1)

c.cc (1.4 KB ) - added by mark.r.buda@… 12 years ago.
demonstration of the problem with serializing protected classes

Download all attachments as: .zip

Change History (7)

by mark.r.buda@…, 12 years ago

Attachment: c.cc added

demonstration of the problem with serializing protected classes

comment:1 by Robert Ramey, 12 years ago

Hmmm, it would be nice if you could share your workaround. Is it something that could be just rolled into export.hpp?

Robert Ramey

comment:2 by mark.r.buda@…, 12 years ago

I took BOOST_CLASS_EXPORT_GUID and split it into two macros, removing the namespace bits:

#define BOOST_CLASS_EXPORT_PROTECTED_1(T)                              \
  static ::boost::archive::detail::guid_initializer< T > const         \
  & guid_initializer;                                                  \

#define BOOST_CLASS_EXPORT_PROTECTED_2(W, T, K)                        \
  ::boost::archive::detail::guid_initializer< W::T > const &           \
  W::guid_initializer =                                                \
           ::boost::serialization::singleton<                          \
               ::boost::archive::detail::guid_initializer< W::T >      \
    >::get_mutable_instance().export_guid(K)                           \
/**/

class foo {
  protected:
    class bar {
    // ...
    };
    BOOST_CLASS_EXPORT_PROTECTED_1(foo::bar)
};

BOOST_CLASS_EXPORT_PROTECTED_2(foo, bar, "foo::bar")

comment:3 by mark.r.buda@…, 12 years ago

This workaround doesn't, in fact, work around. I don't understand what's going on well enough to make it work.

comment:4 by Robert Ramey, 12 years ago

I looked at this a little bit and I don't see any easy fix.

I tried adding another friend to the protected class - but that doesn't work because init_guid is in an anonymous namespace. Of course you CAN work around by either not using a nested class or by making public (which I guess amounts to the same thing). Of course you don't want to do that. For now I'll just leave this open.

Robert Ramey

comment:5 by Robert Ramey, 12 years ago

Status: newassigned

comment:6 by Robert Ramey, 7 years ago

Resolution: fixed
Status: assignedclosed

Note; recently I've had occasion to look in to this. Turns out that one can serialization private and protected base classes - just not through pointers. I've added a case study to the documentation so explain this. I also investigated what it would take to make this work for pointers and concluded that it's not possible in the current library with current C++. So I'm going to close this item.

Note: See TracTickets for help on using tickets.