[Boost-bugs] [Boost C++ Libraries] #4026: Request to trap missing serialize method in derived class(es)

Subject: [Boost-bugs] [Boost C++ Libraries] #4026: Request to trap missing serialize method in derived class(es)
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-03-18 14:19:54


#4026: Request to trap missing serialize method in derived class(es)
-----------------------------------------------+----------------------------
 Reporter: Richard Hazlewood | Owner: ramey
     Type: Feature Requests | Status: new
Milestone: Boost 1.43.0 | Component: serialization
  Version: Boost 1.42.0 | Severity: Problem
 Keywords: serialize version derived missing |
-----------------------------------------------+----------------------------
 Is there a way the serialization library can trap (or warn of) accidental
 serialization of a derived class, where the implementor has not added a
 serialize method for the derived class?


 The "Base Classes" section of the documentation says:
 [[BR]]
 "For this reason, all serialize member functions should be private."
 [[BR]]
 However, this does not help (at least for MSVC71) for cases where the
 serialize method is completely missing in the derived class; the derived
 class is implicitly converted to the base (is-a).


 This case can easily be missed, and results in invalid data as soon as
 versioned data is added to the base class. This is demonstrated in the
 example code, below.


 I don't know if it can be achieved, but at the point of serialization,
 where the library knows the type that is being serialized, it would be
 useful if it could warn if the serialize method being called was not a
 member of the *exact* type (clearly only for class types). If the call is
 falling through to the base then it is probably (always?) an oversight.


 Perhaps if there was a class sitting between 'Base' and 'Derived' then
 that may be a case for not wanting the warning, but it seems like good
 practice to add the serialize method anyway.


 If a warning message is unsuitable, then it would useful to have a
 documentation update along the lines of:
 [[BR]]
 If there is any chance of using polymorphism or changing the version of a
 class then ensure all derived classes implement the serialize method,
 making the base_object<> call at a minimum.
 [[BR]]
 ----
 {{{
 #include <boost/archive/text_oarchive.hpp>
 #include <boost/serialization/serialization.hpp>
 #include <boost/serialization/base_object.hpp>
 #include <sstream>

 class Base
 {
 private:
     friend class boost::serialization::access;

     template <typename A>
     void serialize(A &/*a*/, const unsigned v)
     {
         assert(v == 1); // Uh-oh, we're picking up version of 'Derived'
     }
 };
 BOOST_CLASS_VERSION(Base, 1);

 class Derived : public Base
 {
     // Implementor 'forgets' to add:
     // template <typename A>
     // void serialize(A &a, const unsigned v)
     // {
     // a & boost::serialization::base_object<Base>(*this);
     // }
 };

 int main(int, char *[])
 {
     std::ostringstream ostr;
     boost::archive::text_oarchive oa(ostr);

     Derived d;
     // Ideally we should be forced to:
     // Base &b = d;
     // oa & b;
     // if no serialize method is found for 'Derived'
     oa & d;

     return 0;
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/4026>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:02 UTC