简体   繁体   English

如何使用Boost序列化对Boost scoped_array进行序列化?

[英]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 message: 我正在尝试使用Boost序列化序列化一个Boost scoped_array但是编译器(VS2008)给我以下错误消息:

error C2039: 'serialize' : is not a member of 'boost::scoped_array<T>'

How do I serialize a scoped_array ? 我如何序列化scoped_array Is there a Boost library that I should be including for this? 我应该为此添加Boost库吗?

Guess not. 可能不会。 The scoped_ptr and scoped_array are designed for keeping track of pointers in a local scope. scoped_ptrscoped_array旨在跟踪本地作用域中的指针。

The scoped_ptr template is a simple solution for simple needs. scoped_ptr模板是满足简单需求的简单解决方案。 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. 将内容序列化并稍后再读似乎与该类的意图背道而驰。

序列化数组本身,而不是数组周围的内存管理包装器。

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];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM