|
Boost : |
From: Janek Kozicki (janek.listy.mailowe_at_[hidden])
Date: 2019-07-21 20:24:53
Hi, this simple program will not compile. The only way to make it
work is to make a public default constructor. This defeats the purpose
of boost::serialization::access.
I tried to fix <boost/serialization/optional.hpp> but I couldn't get
it to correctly call the access::construct();
Please help.
// g++ -o serialize_optional /tmp/serialize_optional.cpp -lboost_serialization
#include <fstream>
#include <iostream>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/optional.hpp>
#include <boost/optional.hpp>
class Foo {
public:
Foo(int k) : i(k) {}
friend std::ostream& operator<<(std::ostream& os, Foo const& foo) {
return os << "Foo { " << foo.i << " }";
}
private:
Foo() : i(-1) {};
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int){
#define N(a) BOOST_SERIALIZATION_NVP(a)
ar & N(i);
#undef N
};
int i;
};
int main() {
using Data = boost::optional<Foo>;
std::cout << "Boost version: " << BOOST_VERSION << "\n";
{
auto f = Data(133);
std::ofstream os("test.xml");
boost::archive::xml_oarchive oa(os);
oa << boost::serialization::make_nvp("f", f);
}
{
Data g;
std::ifstream is("test.xml");
boost::archive::xml_iarchive ia(is);
ia >> boost::serialization::make_nvp("f", g);
std::cout << g.get() << "\n";
}
}
best regards
Janek Kozicki
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk