c++谷物存档类型专门化不起作用
C++ Cereal Archive Type Specialization not working
我正在使用vs2013的谷物1.1.2。
我已经尝试了归档专门化的例子从这里的类型:http://uscilab.github.io/cereal/archive_specialization.html
但是它不会编译错误:
error C2665: 'cereal::make_nvp' : none of the 3 overloads could convert all the argument types ... while trying to match the argument list '(const std::string, const std::string)
我正在尝试编译以下代码,使用示例中的片段:
#include "cerealtypesmap.hpp"
namespace cereal
{
//! Saving for std::map<std::string, std::string> for text based archives
// Note that this shows off some internal cereal traits such as EnableIf,
// which will only allow this template to be instantiated if its predicates
// are true
template <class Archive, class C, class A,
traits::EnableIf<traits::is_text_archive<Archive>::value> = traits::sfinae> inline
void save(Archive & ar, std::map<std::string, std::string, C, A> const & map)
{
for (const auto & i : map)
ar(cereal::make_nvp<Archive>(i.first, i.second));
}
//! Loading for std::map<std::string, std::string> for text based archives
template <class Archive, class C, class A,
traits::EnableIf<traits::is_text_archive<Archive>::value> = traits::sfinae> inline
void load(Archive & ar, std::map<std::string, std::string, C, A> & map)
{
map.clear();
auto hint = map.begin();
while (true)
{
const auto namePtr = ar.getNodeName();
if (!namePtr)
break;
std::string key = namePtr;
std::string value; ar(value);
hint = map.emplace_hint(hint, std::move(key), std::move(value));
}
}
} // namespace cereal
#include "cerealarchivesjson.hpp"
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::stringstream ss;
{
cereal::JSONOutputArchive ar(ss);
std::map<std::string, std::string> filter = { { "type", "sensor" }, { "status", "critical" } };
ar(CEREAL_NVP(filter));
}
std::cout << ss.str() << std::endl;
{
cereal::JSONInputArchive ar(ss);
cereal::JSONOutputArchive ar2(std::cout);
std::map<std::string, std::string> filter;
ar(CEREAL_NVP(filter));
ar2(CEREAL_NVP(filter));
}
std::cout << std::endl;
return 0;
}
注意,如果我删除save()函数重载,它会编译。但我的目标是能够使用map键作为JSON键,所以它是这样的:
{
"map": {
"a": 1,
"b": 2
},
"map_not_string": [
{
"key": 1,
"value": 1
},
{
"key": 2,
"value": 2
}
]
}
我在Cereal的Github上提出了这个问题,并得到了答案:https://github.com/USCiLab/cereal/issues/197
问题是make_nvp的模板化版本,所以
ar(cereal::make_nvp<Archive>(i.first, i.second));
是
ar(cereal::make_nvp(i.first, i.second));
文档已经更新了,所以这应该不再是问题了。
相关文章:
- 线程 std::调用未知类型,无法专门化函数错误
- 如何为所有非数组类型专门化模板?
- 如何在同一个模板功能上专门化几种类型?
- 如何为底层类型 int 的枚举专门化类
- 扣除指南的尾随回报类型不是专门化
- C++模板方法专门化联合类型
- 专门化模板数据类型的模板
- 无法专门化 std::hash 以unordered_map存储自定义类型
- 使用非类型模板参数进行专门化模板模板参数
- 使用decltype尾部返回类型专门化函数模板
- 是否可以为模板化类型专门化模板
- 按参数C++类型专门化重载构造函数
- 如何为从特定类型派生的类型专门化模板
- 用泛型返回类型专门化函数
- 如何为<T>用户定义的类型专门化 std::hash?
- 如何为所有引用类型专门化类
- 为一组类型专门化多个模板
- 如何为多个密钥类型专门化std映射
- 如何使用模板类型专门化模板功能
- c++谷物存档类型专门化不起作用