我已经在github上问过这个问题(大约一个月前),没有任何答案,所以我现在在这里问。
我在我的项目中使用 Cereal 作为序列化库。我尝试为std::string_view
(基本上是从std::string
实现中复制和粘贴)添加序列化功能。但是,Cereal 会引发编译器错误:
谷物找不到提供的类型和存档组合的任何输出序列化函数。
这是我的实现(我在这里禁用了反序列化,但我也尝试了一个虚拟函数,它给了我相同的结果):
#pragma once
#include "../cereal.hpp"
#include <string_view>
namespace cereal
{
//! Serialization for basic_string_view types, if binary data is supported
template <class Archive, class CharT, class Traits>
typename std::enable_if<traits::is_output_serializable<BinaryData<CharT>, Archive>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME(Archive& ar, std::basic_string_view<CharT, Traits> const& str)
{
// Save number of chars + the data
ar(make_size_tag(static_cast<size_type>(str.size())));
ar(binary_data(str.data(), str.size() * sizeof(CharT)));
}
//! Deserialization into std::basic_string_view is forbidden due to its properties as a view.
//! However std::basic_string_view can be deserialized into a std::basic_string.
// template <class Archive, class CharT, class Traits>
// void CEREAL_LOAD_FUNCTION_NAME(Archive& ar, std::basic_string_view<CharT, Traits> & str);
}
最小的例子:
#include <iostream>
#include <cereal/string_view>
int main()
{
/*
* Working archive types are:
* - BinaryOutputArchive
* - PortableBinaryOutputArchive
*
* Compiler errors for:
* - JSONOutputArchive
* - XMLOutputArchive
*/
using OutputArchive = cereal::JSONOutputArchive;
std::string_view str = "Hello World!";
{
OutputArchive oar(std::cout);
oar(str);
}
return 0;
}
测试成功编译并通过了二进制存档,但不适用于 XML 和 JSON 序列化。
我认为这与enable_if
condition中的特征有关is_output_serializable<BinaryData<CharT>, Archive>
,但该特征也存在于std::string
实现中并且工作得很好。我也找不到std::string
.
为什么我会收到 XML 和 JSON 档案的编译器错误?