Skip to content

Add explicit move assignment operator #873

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/json/value.h
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 9 additions & 1 deletion src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
@@ -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_;