StaticString effectiness

2012-02-13
2013-05-21
  • Stephen Chu
    Stephen Chu
    2012-02-13

    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?

     
  • Dmitry T.
    Dmitry T.
    2013-05-21

    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.