Just a tid bit of information that doesn't seem documented anywhere. Does anyone know? Because I would like to make it private, hoping that the cons开发者_运维知识库tructor would be called from boost::serialization::access
which is declared as a friend.
Test example. Given that this works I'd assume it is a feature and would be upset if there was a future release that didn't allow the access granting mechanism to grant access to private default constructors.
#include <boost/serialization/access.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <iostream>
struct Colour {
double colour[4];
boost::shared_ptr<Colour> alt;
static boost::shared_ptr<Colour> test() {
return boost::shared_ptr<Colour>(new Colour);
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /*file_version*/) {
ar & colour;
}
Colour() {
std::cout << "Getting called" << std::endl;
}
};
int main() {
boost::shared_ptr<Colour> c = Colour::test();
c->alt = Colour::test();
std::cout << "Created" << std::endl;
std::stringstream str;
boost::archive::text_oarchive oa(str);
oa & c;
std::cout << "Saved" << std::endl;
c.reset();
boost::archive::text_iarchive ia(str);
ia & c;
std::cout << "Restored" << std::endl;
}
(Interestingly it seems to default construct one and then copy construct another on my system).
精彩评论