Skip to content

Move rvalue strings, arrays, and objects on generic assignment - #3116

Merged
stephenberry merged 7 commits into
mainfrom
issue-3105-move-assign
Nov 25, 2026
Merged

stephenberry merged 7 commits into
mainfrom
issue-3105-move-assign

Conversation

@stephenberry

@stephenberry stephenberry commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Fixes #3105.

generic_json only had const& assignment overloads for std::string, array_t, and object_t, so assigning an rvalue of those types copied it. This adds && overloads that move into the stored value, noexcept when the move is (for objects this depends on the map type, since std::map can allocate when moved on MSVC). operator[] now also forwards its key, so an rvalue key is moved into a newly inserted member.

Assignment from a value that lives inside the target, such as json = json[0] or json = std::move(json["child"].get_array()), previously destroyed the source while reading it. The generic_json copy and move assignments and the array and object overloads now build the new value before replacing the stored one.

Tests check that moves keep the source's heap buffer and that assignment from nested values produces the expected result.

Comment thread include/glaze/json/generic_fwd.hpp Outdated
// `json = std::move(json["child"].get_array())`. Replacing `data` can destroy it before it is read, so these
// overloads build the new value first and then move it into `data`.

generic_json& operator=(std::string&& value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

any strong reasons not to declare it noexcept? absence of that will prevent a compiler from some optimizations

Comment thread include/glaze/json/generic_fwd.hpp Outdated
return *this;
}

generic_json& operator=(array_t&& value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

same here

Comment thread include/glaze/json/generic_fwd.hpp Outdated
return *this;
}

generic_json& operator=(object_t&& value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

same here

@aleksej-zimperium aleksej-zimperium left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's all about noexcept specifier, except for this looks ok.

@aleksej-zimperium

Copy link
Copy Markdown

@stephenberry do my comments make sense to you?

@stephenberry

Copy link
Copy Markdown
Owner Author

Yes, thanks. String and array moves can't throw, so those are now noexcept. For object_t the spec is conditional on the map's move, since object_t is user-selectable and glz::generic_sorted uses std::map, whose move constructor can allocate (and throw) on MSVC.

@aleksej-zimperium aleksej-zimperium left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good

@stephenberry
stephenberry merged commit 353e62f into main Nov 25, 2026
66 checks passed
@stephenberry
stephenberry deleted the issue-3105-move-assign branch November 25, 2026 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some move assignment operators are missing and actually copy ones are called.

2 participants