Boost logo

Boost Users :

Subject: Re: [Boost-users] boost_serialization ok with vector<int> but not with vector<int *>
From: Tony Camuso (tcamuso_at_[hidden])
Date: 2015-03-08 21:01:56


On 03/08/2015 08:33 PM, Robert Ramey wrote:
> Tony Camuso wrote
>> On 03/06/2015 07:42 PM, Robert Ramey wrote:
>>> Tony Camuso wrote
>>>> Why is the compiler okay with ...
>>>> std::vector
>>>>
>> <int>
>>>> intvec;
>>>> ... but throws an error for ...
>>>> std::vector
>>>>
>> <int *>
>>>> pintvec;
>>>
>>> The answer can be found here:
>>>
>>>
>
>> &lt;http://&gt;
>>> http://stackoverflow.com/questions/19076299/how-do-i-serialize-a-class-containing-pointers-to-primitives
>>>
>>> I concede that this should be added to the documentation. Feel free to
>>> open a trac item to remind me of this.
>
> actually, I think I can do better than that. I got in the habit of adding a
> layer of static asserts to trap cases where the user breaks some rule or
> another. At the site of the trap, a comment in the code explains what on
> has to do to fix it. Of course this is self defense so I don't have
> continually answer the same questions over and over. Over the years as I get
> a new question, I added traps like this. For some reason this question
> hasn't been asked often enough to trigger my normal behavior. But I think I
> can more or less easily implement this.

You may want to post a FAQ for boost newbies. In your spare time, of course. :)

>>> So the solution is to wrap your int inside a class. This will give it a
>>> unique type and distinguish from all the other int instances you don't
>>> really want to track. Also you can use BOOST_SERIALIZATION to create a
>>> wrapper.
>
> Note: it's BOOST_SERIALIZATION_STRONG_TYPEDEF. It's got it's own special
> section in the manual.
>
> But it only works for numeric types.
>
> If you want to serialize a string pointer you'll have to snooker your system
> like this:
>>
>
> Thanks, Robert.
>
> Wasn't aware of the BOOST_SERIALIZATION macro. I'll look it up.
>
> Another question. It seems that attempts to serialize containers of pointers
> of any kind throw compiler errors. For example,
>
> std::vector<string> str; // compiler is ok with serializing this
>
> std::vector<string *> pstr; // compiler throws an error
>
> The documentation gave me the impression that the serialization lib would
> dereference the pointers correctly, and perform all the housekeeping around
> that. Also got the impression that nothing special had to be done for the
> deserialize step.
>
> That's not correct - this problem occurs for all "primitive types". Look at
> the
> documentation on "serialization level". This basically means all C++
> primitive
> types + std::string. To handle std::string use:
>
>> Is my reading comprehension off? :)
>
> No, but you might want to do more of it.
>

I have it working, now. I was confusing containers of pointers with pointers to
containers. The latter is supported, the former isn't, for reasons that become
obvious when you look at memory with a debugger.

Thanks for your help.

Regards,
Tony


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net