How do I serialize a Boost scoped_array using Boost serialization?
I am trying to serialize a Boost scoped_array
using Boost serialization but the compiler (VS2008) is giving me the following error messa开发者_Go百科ge:
error C2039: 'serialize' : is not a member of 'boost::scoped_array<T>'
How do I serialize a scoped_array
? Is there a Boost library that I should be including for this?
Guess not. The scoped_ptr
and scoped_array
are designed for keeping track of pointers in a local scope.
The scoped_ptr template is a simple solution for simple needs. It supplies a basic "resource acquisition is initialization" facility, without shared-ownership or transfer-of-ownership semantics. Both its name and enforcement of semantics (by being noncopyable) signal its intent to retain ownership solely within the current scope.
Having the content serialized and read back later seems to be against the intent of the class.
Serialise the array itself, not a memory-managing wrapper around it.
Here is a solution that I ended up using (symmetric -- works for saving and loading):
void myClass::serialize(Archive & ar, const unsigned int file_version)
{
ar & myScopedArraySIZE;
// Only gets called during loading
if (Archive::is_loading::value)
{
myScopedArray.reset(new ColourPtr[myScopedArraySIZE]);
}
for (uint i = 0; i < myScopedArraySize; i++)
{
ar & myScopedArray[i];
}
}
精彩评论