Feature: Add constructor support for C++20 Ranges and Views#5018
Feature: Add constructor support for C++20 Ranges and Views#5018DanielHwangbo wants to merge 3 commits into
Conversation
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @DanielHwangbo |
Signed-off-by: Daniel Hwangbo <ghwangbo78@gmail.com>
Signed-off-by: Daniel Hwangbo <ghwangbo78@gmail.com>
Signed-off-by: Daniel Hwangbo <ghwangbo78@gmail.com>
47d9b50 to
1cd971b
Compare
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @DanielHwangbo |
|
This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions! |
nlohmann
left a comment
There was a problem hiding this comment.
Please amalgamate the source code and make sure the added code is only compiled for C++20.
|
This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions! |
Description
This PR introduces a new constructor to
basic_jsonthat enables direct initialization from C++20 Ranges and Views (e.g.,std::views::filter,std::views::transform).Motivation
Currently, attempting to initialize a JSON object from a C++20 view fails because the library does not natively recognize the view type as a container. Users are currently forced to manually pass iterators, which breaks the ergonomics of modern C++20 pipelines.
Feature: Add constructor support for C++20 Ranges and Views
Description
This PR introduces a new constructor to
basic_jsonthat enables direct initialization from C++20 Ranges and Views (e.g.,std::views::filter,std::views::transform).Motivation
Currently, attempting to initialize a JSON object from a C++20 view fails because the library does not natively recognize the view type as a container. Users are currently forced to manually pass iterators, which breaks the ergonomics of modern C++20 pipelines.
Before:
Implementation Details
I added a templated constructor that delegates to the existing (InputIT, InputIT) constructor.
SFINAE & Ambiguity Resolution: To prevent "ambiguous overload" errors (specifically with std::string and std::vector, which satisfy the Range concept but are already handled by the library), I added specific SFINAE guards. The new constructor is disabled via std::enable_if if:
The type is basic_json (preserves copy/move constructor priority).
The type is a json_ref.
The type is already a compatible_type (e.g., std::string), ensuring existing library behavior is preserved and no ambiguity errors occur during compilation.
Checklist
[x] The changes are described in detail, both the what and why.
[ ] If applicable, an existing issue is referenced.
[x] The source code is amalgamated by running make amalgamate.
[x] Verified that std::string initialization still works (no ambiguity).
[ ] The Code coverage remained at 100%. (A new test case may be required).