Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit move assignment operator #873

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/json/value.h
Expand Up @@ -349,7 +349,10 @@ Json::Value obj_value(Json::objectValue); // {}
/// Deep copy, then swap(other).
/// \note Over-write existing comments. To preserve comments, use
/// #swapPayload().
Value& operator=(Value other);
Value& operator=(const Value& other);
#if JSON_HAS_RVALUE_REFERENCES
Value& operator=(Value&& other);
#endif

/// Swap everything.
void swap(Value& other);
Expand Down
10 changes: 9 additions & 1 deletion src/lib_json/json_value.cpp
Expand Up @@ -497,10 +497,18 @@ Value::~Value() {
value_.uint_ = 0;
}

Value& Value::operator=(Value other) {
Value& Value::operator=(const Value& other) {
Value tmp(other);
swap(tmp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapping into a temporary is wasted work.
I think we should rather write "real" operators rather than relying on swap.

I'll work on that now and send a different PR proposal.

return *this;
}

#if JSON_HAS_RVALUE_REFERENCES
Value& Value::operator=(Value&& other) {
swap(other);
return *this;
}
#endif

void Value::swapPayload(Value& other) {
ValueType temp = type_;
Expand Down