只是一些似乎没有记录在任何地方的信息。有人知道吗?因为我想把它设为私有,希望构造函数可以从被声明为友元的boost::serialization::access
调用。
测试示例。考虑到这可以工作,我认为这是一个特性,如果未来的版本不允许访问授予机制授予私有默认构造函数的访问权限,我会感到不安。
#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;
}
(有趣的是,它似乎默认构造一个,然后复制构造另一个在我的系统)。