As part of upgrading a product to a later version of boost the compilation
has tripped up on serialization of a boost::optional<std::string>.
The /optional.hpp/ header now has the comment:
...and this fires for std::string.
What, if any, is the recommended way forward for this?
An example code snippet to illustrate:
Previous version of boost: 1.50; new: 1.6x
Compiling with g++ 4.6.4 (although same problem with 4.8.5): -W -Wall
(The real code is actually using the portable_binary_archive, but let's
avoid muddying the waters at this stage :)
OK, so it looks like posting from nabble loses the raw-tag stuff...
The optional.hpp comment:
// It is an inherent limitation to the serialization of optional.hpp
// that the underlying type must be either a pointer or must have a
// default constructor. It's possible that this could change sometime
// in the future, but for now, one will have to work around it. This
// be done by serialization the optional<T> as optional<T *>
In reply to this post by Boost - Users mailing list
On 2/19/18 4:35 AM, Chardrazle via Boost-users wrote:
> As part of upgrading a product to a later version of boost the compilation
> has tripped up on serialization of a boost::optional<std::string>.
> The /optional.hpp/ header now has the comment:
> ...and this fires for std::string.
> What, if any, is the recommended way forward for this?
int main(int argc, char *argv)
std::ofstream ofs("/tmp/tmp.bin", std::ios::out | flags);
boost::optional<std::string> s = std::string("hello");
oa << s;
std::ifstream ifs("/tmp/tmp.bin", std::ios::in | flags);
iarchive_type ia(ifs); // # CRASH for binary #
ia >> s;
To work around the static_assertion, I tried adding the (serialization)
specialization for is_default_constructible<std::string>.
This then gets everything to compile, and the text_archive version doesn't
have any problems.
However, the binary_archive version crashes when trying to de-serialize.
( basic_binary_iprimitive.ipp(107): s.resize(l); )
I was concerned that the specialization has activated something undesirable,
hence my question about the correct way forward...
On 2/20/18 1:41 AM, Chardrazle via Boost-users wrote:
> Indeed, and my initial workaround was to force that through:
> It may be related to: https://svn.boost.org/trac10/ticket/13108 > (is_default_constructible support.)
> Time to start muddying the waters.
> (Note: the binary archives were taken from the latest serialization 'test'
> code area.)
show serialization of optional passing with all archives - including the
So it seems that this is an issue related to portable_binary_archive
which has the status and example not being currently being tested on a
regular basis. In theory, the code in archive implementations should be
totally agnostic to the data type being serialized. Alas, s*** happens.
I would recommend tracing with the debugger trough serialization of
optional<string> in portable binary archive to see how it's different
from serialization in binary or text archive.
I've been wanting to promote portable_binary_archive to "offically
supported" status, but I can't justify spending the time.
>I've been wanting to promote portable_binary_archive to "offically
>supported" status, but I can't justify spending the time.
This is why I thought I'd better post here, rather than going straight to
I'm glad I did. I performed some further investigation by trying the sample
code on my home set-up, with variations of g++ versions. I found no
problems whatsoever with the portable_binary_archive.
I suspect that whatever configuration has been used at the company has
really messed up something to do with memory management.