static_assert 使用 boost 反序列化二进制数据时失败 "typex::value" 失败
static_assert failed "typex::value" failure while deserialising binary data using boost
下面的代码序列化和反序列化 class 和结构的成员。
序列化工作正常,但我在尝试使用 oarch >> BOOST_SERIALIZATION_NVP(outObj);
反序列化时遇到以下错误
代码中有没有我没有实现也没有实现的大错误。
In file included from main.cpp:1:
In file included from /usr/local/include/boost/archive/binary_oarchive.hpp:21:
In file included from /usr/local/include/boost/archive/binary_oarchive_impl.hpp:22:
In file included from /usr/local/include/boost/archive/basic_binary_oarchive.hpp:33:
In file included from /usr/local/include/boost/archive/detail/common_oarchive.hpp:22:
In file included from /usr/local/include/boost/archive/detail/interface_oarchive.hpp:23:
In file included from /usr/local/include/boost/archive/detail/oserializer.hpp:68:
/usr/local/include/boost/archive/detail/check.hpp:162:5: error: static_assert failed "typex::value"
BOOST_STATIC_ASSERT(typex::value);
^ ~~~~~~~~~~~~
/usr/local/include/boost/static_assert.hpp:70:41: note: expanded from macro 'BOOST_STATIC_ASSERT'
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <map>
#include <boost/serialization/map.hpp>
#include <boost/serialization/split_member.hpp>
struct values
{
std::string name;
std::string sex;
values():name("dummy"),sex("dummy"){} ;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(name);
ar & BOOST_SERIALIZATION_NVP(sex);
}
};
class Myclass
{
public:
Myclass()
{
values val1;
e_group.insert( std::make_pair(1,val1) ) ;
e_group.insert( std::make_pair(2,val1) ) ;
p_group.insert( std::make_pair(1,e_group) ) ;
p_group.insert( std::make_pair(2,e_group) ) ;
}
template<class Archive>
void serialize(Archive & ar, const unsigned int version) const
{
ar & BOOST_SERIALIZATION_NVP(e_group);
ar & BOOST_SERIALIZATION_NVP(p_group);
}
typedef std::map<int,values> groups;
typedef std::map<int,groups> Pgroups;
groups e_group;
Pgroups p_group;
};
int main()
{
Myclass assetlist;
auto os = std::ostringstream(std::ios::binary);
boost::archive::binary_oarchive arch( os, boost::archive::no_header);
arch << BOOST_SERIALIZATION_NVP(assetlist);
std::string s1 = os.str();
std::stringstream is( s1, std::ios_base::binary| std::ios_base::out| std::ios_base::in);
Myclass outObj;
boost::archive::binary_iarchive iarch (is , boost::archive::no_header );
iarch >> BOOST_SERIALIZATION_NVP(outObj);
return 0;
}
由于 Myclass::serialize 函数中的额外 'const',您的静态断言失败。
它应该看起来像:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{ ... }
关于 BOOST_SERIALIZATION_NVP() 用法请参阅 this 问题。
下面的代码序列化和反序列化 class 和结构的成员。
序列化工作正常,但我在尝试使用 oarch >> BOOST_SERIALIZATION_NVP(outObj);
反序列化时遇到以下错误
代码中有没有我没有实现也没有实现的大错误。
In file included from main.cpp:1: In file included from /usr/local/include/boost/archive/binary_oarchive.hpp:21: In file included from /usr/local/include/boost/archive/binary_oarchive_impl.hpp:22: In file included from /usr/local/include/boost/archive/basic_binary_oarchive.hpp:33: In file included from /usr/local/include/boost/archive/detail/common_oarchive.hpp:22: In file included from /usr/local/include/boost/archive/detail/interface_oarchive.hpp:23: In file included from /usr/local/include/boost/archive/detail/oserializer.hpp:68: /usr/local/include/boost/archive/detail/check.hpp:162:5: error: static_assert failed "typex::value" BOOST_STATIC_ASSERT(typex::value); ^ ~~~~~~~~~~~~ /usr/local/include/boost/static_assert.hpp:70:41: note: expanded from macro 'BOOST_STATIC_ASSERT'
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <map>
#include <boost/serialization/map.hpp>
#include <boost/serialization/split_member.hpp>
struct values
{
std::string name;
std::string sex;
values():name("dummy"),sex("dummy"){} ;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(name);
ar & BOOST_SERIALIZATION_NVP(sex);
}
};
class Myclass
{
public:
Myclass()
{
values val1;
e_group.insert( std::make_pair(1,val1) ) ;
e_group.insert( std::make_pair(2,val1) ) ;
p_group.insert( std::make_pair(1,e_group) ) ;
p_group.insert( std::make_pair(2,e_group) ) ;
}
template<class Archive>
void serialize(Archive & ar, const unsigned int version) const
{
ar & BOOST_SERIALIZATION_NVP(e_group);
ar & BOOST_SERIALIZATION_NVP(p_group);
}
typedef std::map<int,values> groups;
typedef std::map<int,groups> Pgroups;
groups e_group;
Pgroups p_group;
};
int main()
{
Myclass assetlist;
auto os = std::ostringstream(std::ios::binary);
boost::archive::binary_oarchive arch( os, boost::archive::no_header);
arch << BOOST_SERIALIZATION_NVP(assetlist);
std::string s1 = os.str();
std::stringstream is( s1, std::ios_base::binary| std::ios_base::out| std::ios_base::in);
Myclass outObj;
boost::archive::binary_iarchive iarch (is , boost::archive::no_header );
iarch >> BOOST_SERIALIZATION_NVP(outObj);
return 0;
}
由于 Myclass::serialize 函数中的额外 'const',您的静态断言失败。 它应该看起来像:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{ ... }
关于 BOOST_SERIALIZATION_NVP() 用法请参阅 this 问题。