在某些情况下,使用私有构造函数对类进行反序列化不适用于Boost 1.58.在1.57它工作得很好.使用Visual Studio 2013编译时,下面的代码提供以下错误消息.
错误信息:
1> SerializeTest.cpp 1>c:\program files (x86)\vc\include\xmemory0(588): error C2248: 'A::A' : cannot access private member declared in class 'A' 1> c:\projects\serializetest\serializetest.cpp(14) : see declaration of 'A::A' 1> c:\projects\serializetest\serializetest.cpp(9) : see declaration of 'A'
码:
#include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/serialization/serialization.hpp> #include <boost/serialization/vector.hpp> #include <vector> #include <sstream> class A { public: A(int i) {} private: A() {} friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar,const unsigned int version) { } }; class B { public: B() {} private: std::vector<A> m_a; friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar,const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(m_a); } }; int main(int argc,char* argv[]) { std::ostringstream os; boost::archive::text_oarchive oa(os); { B b; oa & b; } { std::stringstream myIstream; myIstream.write(os.str().c_str(),os.str().length()); boost::archive::text_iarchive ia(myIstream); B b; ia >> b; } return 0; }
这是Boost回归还是我的疏忽?