mtb mtb - 2 months ago 10
C++ Question

conversion from 'size_t' to 'rapidjson::SizeType'

I have this c++ example code:

void test()
{
rapidjson::Document doc;
doc.SetObject();

const std::string source = "The quick brown fox jumps over the lazy dog";
rapidjson::Value source_val;
source_val.SetString( source.c_str(), source.length(), doc.GetAllocator() );
}


And at compile time, on x64 platform, I get this warning:


warning C4267: 'argument': conversion from
size_t
to
rapidjson::SizeType
, possible loss of data


How can I correctly convert from a string's length (
size_t
) to rapidjson SizeType?

Answer

Per the documentation:

RapidJSON uses 32-bit array/string indices even on 64-bit platforms, instead of using size_t. Users may override the SizeType by defining RAPIDJSON_NO_SIZETYPEDEFINE.