I find StaticString only useful when used as the key of an object. When used as string value of an Json::Value, the string is allocated and copied anyway.
Json::Value v = Json::StaticString("Some text");
v still ends up with a copy of the text that is allocated and duplicated from the const char *. It seems this is the only way to avoid allocation and copy:
Json::Value v(Json::StaticString("Some text"));
But it's really not much use since every time it's assigned to another Json::Value, it's allocated and copied.
Maybe there's some part of the API I missed?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In the Value class constructor, only the pointer to "Some text" is assigned to a class field, and the 'allocated' flag field is set to false to avoid further inconsistent memory de-allocation.
There is no string buffer allocation/copying.
So the StaticString may be efficiently used where the string buffer lifetime is greater than the lifetime of a referring Value (or StaticString) object.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I find StaticString only useful when used as the key of an object. When used as string value of an Json::Value, the string is allocated and copied anyway.
Json::Value v = Json::StaticString("Some text");
v still ends up with a copy of the text that is allocated and duplicated from the const char *. It seems this is the only way to avoid allocation and copy:
Json::Value v(Json::StaticString("Some text"));
But it's really not much use since every time it's assigned to another Json::Value, it's allocated and copied.
Maybe there's some part of the API I missed?
In the Value class constructor, only the pointer to "Some text" is assigned to a class field, and the 'allocated' flag field is set to false to avoid further inconsistent memory de-allocation.
There is no string buffer allocation/copying.
So the StaticString may be efficiently used where the string buffer lifetime is greater than the lifetime of a referring Value (or StaticString) object.