Skip to content

docs: correct erase() empty-range and initializer-list move documentation - #6122

Open
0xmyst3ry wants to merge 1 commit into
nlohmann:developfrom
0xmyst3ry:docs-erase-empty-range-and-moved-elements
Open

0xmyst3ry wants to merge 1 commit into
nlohmann:developfrom
0xmyst3ry:docs-erase-empty-range-and-moved-elements

Conversation

@0xmyst3ry

Copy link
Copy Markdown

Fixes items 1 and 3 of #6114. Item 2 (the recorded assertion outputs in features/assertions.md and json_assert.md) is not part of this PR: it needs the examples recompiled, or a decision to drop file and line numbers from the outputs.

What

  • api/basic_json/erase.md, overload (2): the "empty range is a no-op" sentence now applies to arrays and objects only. For primitive values the range must be exactly [begin(); end()), and the empty ranges [begin(); begin()) and [end(); end()) throw invalid_iterator.204. The 204 condition in the exceptions list now says or, matching the code.
  • api/basic_json/basic_json.md (item 5) and api/basic_json/object.md: the strong guarantee does not cover values moved into the initializer list. They are moved when the list is created, before the constructor runs, and stay moved-from if it throws.

Why
The pages promise behaviour the library does not have. The code is deliberate, so the pages are corrected rather than the code: primitive_range_is_complete in json.hpp is used by both erase(first, last) and the range constructor, and invalid_iterator.204 already documents that only (begin(), end()) is valid for primitives. For the second point, json_ref(value_type&&) moves the rvalue into its own storage when the braced list is built.

How I checked
I read the code paths named above and compared the new wording against them. I did not compile or run the reproducers from the issue. Documentation only, so no test, amalgamation or coverage change.

@0xmyst3ry
0xmyst3ry requested a review from nlohmann as a code owner November 13, 2026 16:11

@nlohmann nlohmann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks! The changes look correct: I checked them against primitive_range_is_complete and json_ref(value_type&&) and ran the reproducers from #6114, which match the new text.

The only thing missing is the DCO sign-off. Could you amend the commit with git commit --amend -s --no-edit and force-push? Then this is good to merge from my side.

Optional nit: item 5 in basic_json.md is about the constructor, where the throwing case is basic_json(init, false, value_t::object) (json j = {std::move(a), 5} just creates an array). The json::object(...) example is still valid since it calls that constructor, so feel free to leave it.


Written by Claude Code.

…tion

erase(first, last) on a primitive value only accepts [begin(); end()); the
empty ranges [begin(); begin()) and [end(); end()) throw invalid_iterator.204,
and the exceptions list now says "or" like the code does.

The strong guarantee of json::object() and the initializer-list constructor
does not cover values moved into the initializer list, which are moved before
the constructor runs.

Part of nlohmann#6114

Signed-off-by: 0xshubh4m <shubhamshakti137@gmail.com>
@0xmyst3ry
0xmyst3ry force-pushed the docs-erase-empty-range-and-moved-elements branch from 1f5fc4d to 00a26cf Compare November 13, 2026 18:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants