Improve error message for const fields #2818
Draft
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.
The goal here is to propose an idea for a better error message, which says what is wrong instead of pointing to some internal implementation detail that does not compile.
As I'm a very novice user of the library and even less familiar with the code itself, I'll just create this draft for someone else to finish / abandon the idea. I'm not familiar enough with it to create a proper pull request. Sorry!
It should even be possible to test the static_assert, but that is another week of fiddling.
e.g. https://www.youtube.com/watch?v=zxDzMjfsgjg
The result is certainly not perfect, but it's significantly improved in both tested compilers.
Note: using enable_if or require doesn't really improve the situation.
It just says
error: no matching member function for call to 'get_to'
And like 20 lines later
../single_include/nlohmann/json.hpp:20258:16: note: because 'const int' does not satisfy 'Writeable'
Scenario:
Output with g++ 9.3.0 without the patch:
Output with g++ 9.3.0 with the patch:
Output with clang++ 10.0.0 without the patch:
Output with clang++ 10.0.0 with the patch:
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.Please don't
#ifdef
s or other means.