c++ template for rapidjson::Value

c++ template for rapidjson::Value

本文关键字:Value rapidjson for template c++      更新时间:2024-09-30

我试图创建一个模板来实现将数据设置到数组的功能。我把它做成这样:

template <class T1>
void SetArray(rapidjson::Document &JsonObj, std::string ArrayName, T1 value)
{
if (!JsonObj.IsObject())
JsonObj.SetObject();

rapidjson::Document::AllocatorType& alloc = JsonObj.GetAllocator();
rapidjson::Value KeyPart;
KeyPart.SetString(ArrayName.c_str(), alloc);
rapidjson::Value ValuePart;
ValuePart.SetString(value.c_str(), alloc);
}

我找不到如何通过T1来改变值类型(std::string,int,bool等(请帮忙,谢谢!

rapidjson::Value构造函数已经为各种类型加载了重载,所以您应该只执行

rapidjson::Value ValuePart{value};

并让编译器计算出来。