c++ - boost::serialization with mutable members -


Boost: "best" way of sorting an object with cached, derived values ​​in uncommon members, using serialization What is it? (Example: float): number (n), sqrt_num (-1.0) {} // calculation and cache previously read float get_sqrt () const {if (Sqrt_num & lt; 0) sqrt_num = sqrt (num); Return sqrt_num; } Template & lt; Classic Collections & gt; Zero serial (archive and ar, unsigned int version) {...} Private: Float number; Floating float sqrt_num; };

I want to save separately from splitting serialization (), because for maintenance reasons

a sub-implementation of the serial :

  template & lt; Class archive & gt; Zero serial (reserved and ar, unsigned int version) {R & amp; Number; Sqrt_num = -1.0; }  

It handles the deserialization case, but in the case of the serialization, the cached value is killed and it must be reconnected.

What is the best practice in this case? Splitting your savings and loading methods does not mean that you have to maintain two copies of your serialization code. You can divide them and then they can rejoin them with a normal function.

  Private: Friend class promotion :: Serialization :: Access; BOOST_SERIALIZATION_SPLIT_MEMBER () template & lt; Classroom collection & gt; Zero Save (archive and AR, const. Unsigned int version) const {const_cast & lt; Example * & gt; (This) - & gt; Common_cierialize (AR, version); } Template & lt; Classic Collections & gt; Zero load (archive and r, consigned unsigned edition) {general_cyrealize (AR, version); Sqrt_num = -1; } Template & lt; Classic Collections & gt; Empty general_irialize (archive and ar, cost unsigned edition) {r & amp; Number; }  

You probably noticed const_cast . This is an unfortunate warning for this idea. Although serialize member is non-present for job saving operation, but the save member function needs to be constrained unless you have serial The object being done was not originally announced, however, as it is shown above it is safe to remove documentation; It's similar.

With the above changes, your code will print correctly for both "code" 2 ex1 and ex2 , and you only have to maintain A copy of the ordering code is assigned the code to restart the internal cache of the object only in the code load ; The Save function does not touch the cache.


Comments