Boost serialization doesn't work with shared_ptr

后端 未结 1 1333
执念已碎
执念已碎 2021-01-21 01:08

The following piece of code compiles just fine:

#include 
#include 
#include <         


        
相关标签:
1条回答
  • 2021-01-21 01:35

    From the Boost source code around that assertion:

    // The most common cause of trapping here would be serializing
    // something like shared_ptr<int>.  This occurs because int
    // is never tracked by default.  Wrap int in a trackable type
    BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
    

    Basically, in order to serialize things like shared_ptr properly, the pointed-to objects need to be centrally tracked during the serialization process (to identify when multiple pointers point to the same object, so they don't lead to two copies of the object being serialized). Tracking an object is more expensive than not tracking it, though, so primitive types are not tracked (the assumption being that there's gonna be a lot of them). Essentially, this makes it impossible to serialize shared_ptr<primitive_type> without mucking with the Boost sources. The solution, as the comment says, is to instead serialize some UDT containing an int.

    0 讨论(0)
提交回复
热议问题