Repository navigation
Move rvalue strings, arrays, and objects on generic assignment - #3116
Merged
Merged
Conversation
| // `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) |
There was a problem hiding this comment.
any strong reasons not to declare it noexcept? absence of that will prevent a compiler from some optimizations
| return *this; | ||
| } | ||
|
|
||
| generic_json& operator=(array_t&& value) |
| return *this; | ||
| } | ||
|
|
||
| generic_json& operator=(object_t&& value) |
aleksej-zimperium
left a comment
There was a problem hiding this comment.
That's all about noexcept specifier, except for this looks ok.
|
@stephenberry do my comments make sense to you? |
Owner
Author
|
Yes, thanks. String and array moves can't throw, so those are now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3105.
generic_jsononly hadconst&assignment overloads forstd::string,array_t, andobject_t, so assigning an rvalue of those types copied it. This adds&&overloads that move into the stored value,noexceptwhen the move is (for objects this depends on the map type, sincestd::mapcan 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]orjson = std::move(json["child"].get_array()), previously destroyed the source while reading it. Thegeneric_jsoncopy 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.