【问题标题】:How to symmetrically implement serialize and deserialize template functions in C++如何在 C++ 中对称地实现序列化和反序列化模板函数
【发布时间】:2015-09-20 21:50:33
【问题描述】:

我想编写一系列模板函数来序列化和反序列化对象。我已经完成了序列化部分,一切正常:

#ifndef SERIALIZE_H
#define SERIALIZE_H

#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <memory>

inline std::string to_json(int value) {
    return std::to_string(value);
}


inline std::string to_json(long value) {
    return std::to_string(value);
}


inline std::string to_json(double value) {
    return std::to_string(value);
}


inline std::string to_json(const std::string& myStr) {
    return "\"" + myStr + "\"";
}


template <typename T>
std::string to_json(const std::vector<T>& vec) {
    std::string json("[");

    for(auto &i : vec) {
        json += to_json(i);
        json += ",";
    }

    if (!vec.empty()) json.pop_back();
    json += "]";
    return json;
}


template <typename T>
std::string to_json(const std::unordered_set<T>& mySet) {
    std::string json("[");

    for(const auto& i : mySet) {
        json += to_json(i);
        json += ",";
    }

    if (!mySet.empty()) json.pop_back();
    json += "]";
    return json;
}


template <typename K, typename V>
std::string to_json(const std::unordered_map<K, V>& myMap) {
    std::string json("{");

    for(const auto& i : myMap) {
        json += to_json(i.first);
        json += ":";
        json += to_json(i.second);
        json += ",";
    }

    if (!myMap.empty()) json.pop_back();
    json += "}";
    return json;
}

#endif //SERIALIZE_H

这个serialize.h可以序列化各种组合,比如unordered_map&lt;string, vector&lt;int&gt;&gt;

现在不知道如何递归实现反序列化函数来支持任意组合。

以下是我的deserialize.h 不起作用:

#ifndef DESERIALIZE_H
#define DESERIALIZE_H

#include <string>
#include <rapidjson/document.h>


template<typename T>
T from_json(const std::string &json);

template<>
int from_json(const std::string &json) {
    return std::stoi(json);
}

template<>
long from_json(const std::string &json) {
    return std::stol(json);
}

template<>
double from_json(const std::string &json) {
    return std::stod(json);
}

template<>
std::string from_json(const std::string &json) {
    return json.substr(1, json.size()-1);
}

//
template<typename T>
std::vector<T> from_json(const std::string& json) {
    rapidjson::Value jsonValue;
    {
        const std::string &input = "{\"input\":" + json + "}";
        rapidjson::Document document;
        document.Parse(input.c_str());
        jsonValue = document["input"];
    };
    std::vector<T> vec;
    assert(jsonValue.IsArray());

    for (rapidjson::SizeType i = 0; i < jsonValue.Size(); i++) {
        int element = from_json<T>(std::string(jsonValue[i].GetString()));
        vec.push_back(element);
    }
    return vec;
}

#endif //DESERIALIZE_H

rapidjson 是一个 C++ JSON 库,https://github.com/miloyip/rapidjson

如果我尝试反序列化一个 JSON 字符串:

#include "deserialize.h>

int main() {
    auto vec1 = from_json<std::vector<int>>(std::string("[1,2,3]"));
    return 0;
}

会抛出编译错误:

error: call of overloaded ‘from_json(std::string)’ is ambiguous

似乎没有办法像序列化那样简单地实现反序列化功能。

有什么想法吗?

【问题讨论】:

  • 相关:gotw.ca/publications/mill17.htm 。具体来说,您定义的两个(主要)函数模板之间没有更专业的关系,因为它们仅在返回类型上有所不同。如果您想保留接口,一个解决方案是调度到一些template&lt;typename T&gt; T from_json(tag&lt;T&gt;, const std::string&amp;); template&lt;typename T&gt; std::vector&lt;T&gt; from_json(tag&lt;std::vector&lt;T&gt;&gt;, const std::string&amp;); 重载集,其中可以根据第一个(虚拟/标签)参数建立更专业的关系。
  • 我不会费心让那些内联。转换字符串不是一个简单的操作。
  • 您希望它调用template&lt;typename T&gt; T from_json(const std::string &amp;json);,还是template&lt;typename T&gt; std::vector&lt;T&gt; from_json(const std::string&amp; json)?也就是说,它应该返回std::vector&lt;int&gt;(来自已声明但未定义的第一个模板)还是std::vector&lt;std::vector&lt;int&gt;&gt;(来自第二个模板)?

标签: c++ c++11 template-specialization overloading explicit-specialization


【解决方案1】:

您每次都尝试使用相同的参数和不同的返回类型重载相同的函数 (from_json(...))。这是不合法的。 对于重载,您需要不同的参数类型或参数编号。

Instead of
template<>
double from_json(const std::string &json) {
    return std::stod(json);
}

template<>
std::string from_json(const std::string &json) {
    return json.substr(1, json.size()-1);
}

也许试试这个,或者至少这应该是一般的想法:

template<>
void from_json(const std::string &json, double *out_value) {
    *out_value = std::stod(json);
}

template<>
void from_json(const std::string &json, std::string *out_value) {
    out_value = json.substr(1, json.size()-1);
}

我确定有错误,但我认为这样可以工作(如果你修复它们)

【讨论】:

  • OP 将 函数模板显式特化函数重载 混合在一起。特别是。 template&lt;&gt; double from_hson(const std::string &amp;json);template&lt;typename T&gt; T from_json(const std::string &amp;json); 主模板的明确特化。
  • 使用不同的参数类型会起作用,这是肯定的,但我仍然想对返回类型进行模板特化。
猜你喜欢
  • 1970-01-01
  • 2011-09-26
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多