You shouldn't call serialize
methods directly: operator >>
for an archive does way more than just calling serialize
; depending on the type of archive it first needs to load a preamble etc. You can verify this by stepping through with the debugger, or by checking what's inside test.archive, it is something like
22 serialization::archive 12 0 0 1.000000000e+000 2.000000000e+000
so right after constructing a text_iarchive
the first two calls to operator &
will happen to see those 2 0
's in there instead of the actual data.
Your constructor should be:
template<class TArchive>
Point(TArchive& archive)
{
archive >> *this;
}
Edit here's an example of how to use SFINAE to make sure the copy constructor can still be invoked
Point( const Point& rh ) :
mX( rh.mX ),
mY( rh.mY )
{
}
template<class TArchive>
Point( TArchive& archive,
std::enable_if_t< !std::is_same< TArchive, Point >::value >* = nullptr )
{
archive >> *this;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…