diff --git a/docs/mkdocs/docs/api/basic_json/basic_json.md b/docs/mkdocs/docs/api/basic_json/basic_json.md index e2e73612c3..7be278fbd9 100644 --- a/docs/mkdocs/docs/api/basic_json/basic_json.md +++ b/docs/mkdocs/docs/api/basic_json/basic_json.md @@ -9,7 +9,7 @@ basic_json(std::nullptr_t = nullptr) noexcept; // (3) template -basic_json(CompatibleType&& val) noexcept(noexcept( +JSON_EXPLICIT basic_json(CompatibleType&& val) noexcept(noexcept( JSONSerializer::to_json(std::declval(), std::forward(val)))); @@ -58,6 +58,7 @@ basic_json(basic_json&& other) noexcept; 3. This is a "catch all" constructor for all compatible JSON types; that is, types for which a `to_json()` method exists. The constructor forwards the parameter `val` to that method (to `json_serializer::to_json` method with `U = uncvref_t`, to be exact). + See [Notes](#notes) for the meaning of `JSON_EXPLICIT`. Template type `CompatibleType` includes, but is not limited to, the following types: @@ -239,6 +240,38 @@ basic_json(basic_json&& other) noexcept; ## Notes +- Overload 3: + + !!! note "Definition of `JSON_EXPLICIT`" + + By default, `JSON_EXPLICIT` is defined to the empty string, so the signature is: + + ```cpp + template + basic_json(CompatibleType&& val) + ``` + + If [`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md) is set to `0`, + `JSON_EXPLICIT` is defined to `#!cpp explicit`: + + ```cpp + template + explicit basic_json(CompatibleType&& val) + ``` + + That is, implicit conversions can be switched off by defining + [`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md) to `0`. + + !!! info "Future behavior change" + + Implicit conversions will be switched off by default in the next major release of the library. That is, + `JSON_EXPLICIT` will be set to `#!cpp explicit` by default. + + You can prepare existing code by already defining + [`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md) to `0` and replace any implicit + conversions with explicit calls to [the constructor](../basic_json/basic_json.md) or `static_cast`. + + - Overload 5: !!! note "Empty initializer list" diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 31ca64539b..f5cf59897b 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -832,7 +832,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec typename U = detail::uncvref_t, detail::enable_if_t < !detail::is_basic_json::value && detail::is_compatible_type::value, int > = 0 > - basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape) + JSON_EXPLICIT basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape) JSONSerializer::to_json(std::declval(), std::forward(val)))) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a858728c4c..3efb5781cb 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20135,7 +20135,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec typename U = detail::uncvref_t, detail::enable_if_t < !detail::is_basic_json::value && detail::is_compatible_type::value, int > = 0 > - basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape) + JSON_EXPLICIT basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape) JSONSerializer::to_json(std::declval(), std::forward(val)))) {