A function like this:
std::string getJson()
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
// calling writer events....
return {buffer.GetString(), buffer.GetSize()};
}
is not an option as it will deep copy the content of buffer to the returning std::string, while the point is to reuse the content.
Changing the return type to const char * and just doing return buffer.GetString(); (or changing the return type to std::string_view and just doing return {buffer.GetString(), buffer.GetSize()};), is not an option as well as it seems the destructor of buffer deallocates the internal storage so the returned pointer will actually be dead.
The question is how can I prevent the deallocation of the content by sort of "taking" the ownership from buffer?
Aucun commentaire:
Enregistrer un commentaire