Skip to content

Any plan to add move assignment operator to Json::Value #621

Closed
@gitpaladin

Description

@gitpaladin

In the master branch, Json::Value has move constructor but move assignment operator is missing. So it's very inconvenient to move a existing Value object to a parent node. E.g.

Json::Value child("abc");
Json::Value parent;

// Use swap to void deep copy node
Json::Value &parentSubNodeReference = parent["subNode"];
parentSubNodeReference.swap(child);

// If there is a move assignment operator, it will become very easy
parent["subNode"] = std::move(child);

Additionally, add move assignment operator can solve function return value deep copy problem. E.g.

Json::Value SomeAwesomeFoo()
{
   Json::Value someHugeNode;
   // process some huge node.....omitted
   return someHugeNode;
}

void main()
{
   Json::Value parent;

  // Use swap to prevent deep copy node.
  Json::Value child = SomeAwesomeFoo();  // RVO will prevent deep copy child object
  parent["subNode"].swap(child);

  // This cannot be compiled because swap cannot hold a r-value reference
  // parent["subNode"].swap(SomeAwesomeFoo()); 

  // If there is a move assignment operator, it will become very easy
  // The function return a r-value, the assignment operator will use move assignment 
  // operator to move the temporary object to the subNode
  parent["subNode"] = SomeAwesomeFoo();
}

On the other hand, append method should also have a overloaded version, which accepts r-value reference of Json::Value . So move a temporary object to a array value will come very easy.

Json::Value child("abc");
Json::Value parent(Json::arrayValue);

// Use swap to prevent deep copy node.
Json::Value &parentSubNodeReference = parent.append(Json::objectValue);
parentSubNodeReference.swap(child);

// Use r-value reference version
parent.append(std::move(child));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions