Boost logo

Boost Users :

Subject: Re: [Boost-users] Serialization of optional<std::string>
From: Chardrazle (boost_at_[hidden])
Date: 2018-02-19 12:41:09


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
can
    // be done by serialization the optional<T> as optional<T *>
    BOOST_STATIC_ASSERT(
        boost::serialization::detail::is_default_constructible<T>::value
        || boost::is_pointer<T>::value
    );

The code snippet:
#include <boost/serialization/string.hpp>
#include <boost/serialization/optional.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <fstream>

int main(int argc, char *argv[])
{
    (void)argc; (void)argv;

    typedef boost::archive::text_oarchive oarchive_type;
    typedef boost::archive::text_iarchive iarchive_type;
    std::ios_base::openmode flags = std::ios_base::openmode();

    {
        std::ofstream ofs("/tmp/tmp.bin", std::ios::out | flags);
        boost::optional<std::string> s = std::string("hello");
        oarchive_type oa(ofs);
        oa << s;
    }
    {
        std::ifstream ifs("/tmp/tmp.bin", std::ios::in | flags);
        boost::optional<std::string> s;
        iarchive_type ia(ifs); // # ERROR #
        ia >> s;
    }
}

--
Sent from: http://boost.2283326.n4.nabble.com/Boost-Users-f2553780.html

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