Closed iFreilicht closed 2 months ago
As I thought, this will not work easily because a lot of code depends on implicit construction.
Ah yes, it seems that the CI is running more tests that I didn't run locally. I'll have a look at that.
Could we use a new define JSON_USE_IMPLICIT_CONSTRUCTORS
for this that defaults to true so that users can opt in when they're ready?
Good idea.
So, after playing whack-a-mole with the tests for 2 hours, I think just making the constructor explicit is not enough, it breaks an incredible amount of features, including safe ones (see the latest WIP commit).
A lot of functions already rely on the implicit conversion, so it seems necessary to add overloads for CompatibleTypes
to many functions that then convert internally. I'm not sure that's much better than the current state of things.
Maybe there's a smarter way to prevent nested temporary values from being implicitly converted? Maybe if they're required to be moved instead of allowing const references?
I will not continuing work on this. If someone else is interested in picking it up, feel free to do so.
Previously,
JSON_USE_IMPLICIT_CONVERSIONS
only prevented implicit conversions frombasic_json
to other types.With this change, implicit conversions to
basic_json
are also prevented by that macro.Fixes #2226.
I tested this change also in https://github.com/iFreilicht/json-questions/commit/c0b2f53ebe279b6efd933a73e2b26f1bf2030261, the original repro repo from https://github.com/nlohmann/json/issues/2226#issuecomment-2023142182.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filessingle_include/nlohmann/json.hpp
andsingle_include/nlohmann/json_fwd.hpp
. The whole process is described here.