C++ cereal库使用记录
以下代码报错,报错原因是cereal不支持原生指针
{
auto screamInfo1 = new ConfigInfo::ScreamInfo();
ConfigInfo::ScreamInfo* screamInfo2 = new ConfigInfo::ScreamInfo();
ConfigInfo::ScreamInfo& screamInfo3 = *screamInfo2;
std::ofstream file("screamInfo.json");
cereal::JSONOutputArchive archive(file);
// 保存时的名字为“*screamInfo1”,如果需要名字中不带*号可以使用引用包裹起来。
// cereal不支持原生指针
archive(CEREAL_NVP(screamInfo1), CEREAL_NVP(screamInfo2));
archive(CEREAL_NVP(screamInfo3));
}
{
auto screamInfo1 = new ConfigInfo::ScreamInfo();
ConfigInfo::ScreamInfo* screamInfo2 = new ConfigInfo::ScreamInfo();
ConfigInfo::ScreamInfo& screamInfo3 = *screamInfo2;
memset(screamInfo1, 0, sizeof(ConfigInfo::ScreamInfo));
memset(screamInfo2, 0, sizeof(ConfigInfo::ScreamInfo));
memset(&screamInfo3, 0, sizeof(ConfigInfo::ScreamInfo));
std::ifstream file_in("screamInfo.json");
cereal::JSONInputArchive archive(file_in);
// 加载
archive(CEREAL_NVP(screamInfo1), CEREAL_NVP(screamInfo2));
archive(CEREAL_NVP(screamInfo3));
file_in.close();
}
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp:114:67: note: 'std::integral_constant<bool, false>::value' evaluates to false
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp: In instantiation of 'void cereal::serialize(Archive&, T*&) [with Archive = cereal::JSONOutputArchive; T = ConfigInfo::ScreamInfo]':
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:516:39: required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = ConfigInfo::ScreamInfo*; typename cereal::traits::detail::EnableIfHelper<cereal::traits::has_non_member_serialize<T, ArchiveType>::value, (! cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value), (cereal::traits::is_output_serializable<T, ArchiveType>::value && (cereal::traits::is_specialized_non_member_serialize<T, ArchiveType>::value || (! cereal::traits::is_specialized<T, ArchiveType>::value)))>::type <anonymous> = (cereal::traits::detail::sfinae)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:444:26: required from 'void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = ConfigInfo::ScreamInfo*&; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:333:22: required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {ConfigInfo::ScreamInfo*&}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/archives/JSON.hpp:966:7: required from 'void cereal::save(cereal::JSONOutputArchive&, const cereal::NameValuePair<T>&) [with T = ConfigInfo::ScreamInfo*&]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:532:34: required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = cereal::NameValuePair<ConfigInfo::ScreamInfo*&>; typename cereal::traits::detail::EnableIfHelper<cereal::traits::has_non_member_save<T, ArchiveType>::value, (! cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value), (cereal::traits::is_output_serializable<T, ArchiveType>::value && (cereal::traits::is_specialized_non_member_save<T, ArchiveType>::value || (! cereal::traits::is_specialized<T, ArchiveType>::value)))>::type <anonymous> = (cereal::traits::detail::sfinae)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:444:26: required from 'void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = cereal::NameValuePair<ConfigInfo::ScreamInfo*&>; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:452:22: required from 'void cereal::OutputArchive<ArchiveType, Flags>::process(T&&, Other&& ...) [with T = cereal::NameValuePair<ConfigInfo::ScreamInfo*&>; Other = {cereal::NameValuePair<ConfigInfo::ScreamInfo*&>}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:/apps/IR_Touch/lib/cereal/cereal.hpp:333:22: required from 'ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {cereal::NameValuePair<ConfigInfo::ScreamInfo*&>, cereal::NameValuePair<ConfigInfo::ScreamInfo*&>}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]'
[build] D:\apps\IR_Touch\mainwindow.cpp:49:16: required from here
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp:114:67: error: static assertion failed: Cereal does not support serializing raw pointers - please use a smart pointer
[build] D:/apps/IR_Touch/lib/cereal/types/common.hpp:114:67: note: 'std::integral_constant<bool, false>::value' evaluates to false
[build] mingw32-make.exe[2]: *** [CMakeFiles\IR_Touch.dir\build.make:113: CMakeFiles/IR_Touch.dir/mainwindow.cpp.obj] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:85: CMakeFiles/IR_Touch.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:135: all] Error 2
此外,调用序列化的方法中的CEREAL_NVP
宏由于直接讲变量明转为字符串:#T
,所以在保存时的名字为“*screamInfo”,如果需要导出的Json中变量名中不带*号,可以使用引用包裹起来。
#define CEREAL_NVP(T) ::cereal::make_nvp(#T, T)