diff --git a/doc/mkdocs/docs/api/basic_json/at.md b/doc/mkdocs/docs/api/basic_json/at.md index 436d98f65..635a6efc5 100644 --- a/doc/mkdocs/docs/api/basic_json/at.md +++ b/doc/mkdocs/docs/api/basic_json/at.md @@ -23,7 +23,8 @@ const_reference at(const json_pointer& ptr) const; ## Template parameters `KeyT` -: A type convertible to an object key. This can also be a string literal or a string view (C++17). +: A type for an object key other than `basic_json::json_pointer`. This can also be a string literal or a string view + (C++17). ## Parameters diff --git a/doc/mkdocs/docs/api/basic_json/operator[].md b/doc/mkdocs/docs/api/basic_json/operator[].md index 6c7645e49..20b98e0b9 100644 --- a/doc/mkdocs/docs/api/basic_json/operator[].md +++ b/doc/mkdocs/docs/api/basic_json/operator[].md @@ -27,7 +27,8 @@ const_reference operator[](const json_pointer& ptr) const; ## Template parameters `KeyT` -: A type convertible to an object key. This can also be a string literal or a string view (C++17). +: A type for an object key other than `basic_json::json_pointer`. This can also be a string literal or a string view + (C++17). `T` : string literal convertible to `object_t::key_type` diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b72ed5c8a..225100270 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3494,10 +3494,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec can be thrown.,at__object_t_key_type} */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > reference at(KeyT && key) { // at only works for objects @@ -3546,10 +3547,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec at__object_t_key_type_const} */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > const_reference at(KeyT && key) const { // at only works for objects @@ -3684,10 +3686,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > reference operator[](KeyT && key) { // implicitly convert null value to an empty object @@ -3739,17 +3742,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > const_reference operator[](KeyT && key) const { // const operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - JSON_ASSERT(m_value.object->find(std::forward(key)) != m_value.object->end()); - return m_value.object->find(key)->second; + auto it = m_value.object->find(std::forward(key)); + JSON_ASSERT(it != m_value.object->end()); + return it->second; } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7f74f0b9d..253349a03 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20307,10 +20307,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec can be thrown.,at__object_t_key_type} */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > reference at(KeyT && key) { // at only works for objects @@ -20359,10 +20360,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec at__object_t_key_type_const} */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > const_reference at(KeyT && key) const { // at only works for objects @@ -20497,10 +20499,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > reference operator[](KeyT && key) { // implicitly convert null value to an empty object @@ -20552,17 +20555,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ template < class KeyT, typename std::enable_if < + !std::is_same::type, json_pointer>::value&& ( #if defined(JSON_HAS_CPP_17) - std::is_same::value || + std::is_same::value || #endif - std::is_convertible::value, int >::type = 0 > + std::is_convertible::value), int >::type = 0 > const_reference operator[](KeyT && key) const { // const operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) { - JSON_ASSERT(m_value.object->find(std::forward(key)) != m_value.object->end()); - return m_value.object->find(key)->second; + auto it = m_value.object->find(std::forward(key)); + JSON_ASSERT(it != m_value.object->end()); + return it->second; } JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));