How to boost::serialize an std/boost::optional?
Asked Answered
P

2

9

How can I serialize a class (with boost::serialization) that contains a boost::optional?

I.e. the following code will give an error when instantiated.

error C2039: 'serialize' : is not a member of 'boost::optional' C:\boost\boost_1_55_0\boost\serialization\access.hpp 118

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class MyClass {
private:
    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & my_member;
    }

    boost::optional<int> my_member;
};

int main() {
    std::ofstream ofs("filename.txt");
    const MyClass g;
    boost::archive::text_oarchive oa(ofs);
    oa << g;
    return 0;
}

I understand there's probably a deeper question involved (what should you write to the file when the value is not present?), but there must be some standard solution for it. I am looking for the most simple way to solve this.

Placia answered 26/9, 2014 at 14:27 Comment(1)
boost.org/doc/libs/1_56_0/boost/serialization/optional.hpp You could then look to implement something suitable for std optional as well.Eardrop
Y
10

For boost::optional you just need to add #include <boost/serialization/optional.hpp>

It implements a non-member serialize function that will allow you to serialize boost::optional without worrying about the details.

Under the hood it first saves/loads the boolean value of t.is_initialized() and depending on its value decides if to save/load the rest.

You can see the source code here: http://www.boost.org/doc/libs/1_56_0/boost/serialization/optional.hpp

Youngling answered 26/9, 2014 at 14:33 Comment(8)
I am actually pretty amazed that this didn't turn up in my Google search. I was quite thorough and it looked like I was the only one with this question, which surprised me a lot. Having a look again now, the header indeed pops up - I guess I searched too "question-like".Placia
Just for any future reader, it doesn't seem to work with xml_[i|o]archive, it gives a lengthy template error. Text- and binary archives work fine though.Placia
Actually, loading a file with optionals with binary_iarchive doesn't work either, it produces a runtime error R6010. I might follow it up later in a new question but at the moment I'm happy with text_archive.Placia
@Placia xmlarchive needs node names. Use make_nvpair or BOOST_SERIALIZATION_NV_PAIR or similar (quoting from memory as I'm on very slow mobile network right now).Petey
@sehe: I already tried BOOST_SERIALIZATION_NVP yesterday, it didn't make a change, the xml serialization still didn't work with boost::optional.Placia
@Placia back within reach of internet: coliru.stacked-crooked.com/a/72837fb0a5aa006ePetey
@Youngling - does this work for std::experimental::optional too?Vanegas
@Vanegas cereal has pretty good support for std::optional in its devel branch - and I'm not sure it supports std::experimental::optional out-of-the-box but if not it can be very easily added.Placia
C
0

If you need to serialize boost::optional, you'd just need to include boost/serialization/optional.hpp as already mentioned in odedsh's answer.

However, if you'd like to serialize std::optional, the simplest way is to upgrade to at least version 1.84, where they implemented std::optional support: https://www.boost.org/doc/libs/1_84_0/boost/serialization/optional.hpp

Cytotaxonomy answered 8/8, 2024 at 14:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.