diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index fc438bd87..60a780048 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3680,7 +3680,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ - reference operator[](const typename object_t::key_type& key) + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + reference operator[](const KeyType& key) { // implicitly convert null value to an empty object if (is_null()) @@ -3729,7 +3734,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ - const_reference operator[](const typename object_t::key_type& key) const + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + const_reference operator[](const KeyType& key) const { // const operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) @@ -3884,10 +3894,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ // using std::is_convertible in a std::enable_if will fail when using explicit conversions - template < class ValueType, typename std::enable_if < + template < class KeyType, class ValueType, typename std::enable_if < detail::is_getable::value - && !std::is_same::value, int >::type = 0 > - ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const + && !std::is_same::value&& ( +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value), int >::type = 0 > + ValueType value(const KeyType& key, const ValueType& default_value) const { // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) @@ -3909,7 +3923,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @brief overload for a default value of type const char* @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const */ - string_t value(const typename object_t::key_type& key, const char* default_value) const + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + string_t value(const KeyType& key, const char* default_value) const { return value(key, string_t(default_value)); } @@ -4332,7 +4351,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ - size_type erase(const typename object_t::key_type& key) + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + size_type erase(const KeyType& key) { // this erase only works for objects if (JSON_HEDLEY_LIKELY(is_object())) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 790fa47b0..a7c81dbdd 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20493,7 +20493,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ - reference operator[](const typename object_t::key_type& key) + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + reference operator[](const KeyType& key) { // implicitly convert null value to an empty object if (is_null()) @@ -20542,7 +20547,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ - const_reference operator[](const typename object_t::key_type& key) const + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + const_reference operator[](const KeyType& key) const { // const operator[] only works for objects if (JSON_HEDLEY_LIKELY(is_object())) @@ -20697,10 +20707,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ // using std::is_convertible in a std::enable_if will fail when using explicit conversions - template < class ValueType, typename std::enable_if < + template < class KeyType, class ValueType, typename std::enable_if < detail::is_getable::value - && !std::is_same::value, int >::type = 0 > - ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const + && !std::is_same::value&& ( +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value), int >::type = 0 > + ValueType value(const KeyType& key, const ValueType& default_value) const { // at only works for objects if (JSON_HEDLEY_LIKELY(is_object())) @@ -20722,7 +20736,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @brief overload for a default value of type const char* @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const */ - string_t value(const typename object_t::key_type& key, const char* default_value) const + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + string_t value(const KeyType& key, const char* default_value) const { return value(key, string_t(default_value)); } @@ -21145,7 +21164,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec @since version 1.0.0 */ - size_type erase(const typename object_t::key_type& key) + template < class KeyType, typename std::enable_if < +#if defined(JSON_HAS_CPP_17) + std::is_same::value || +#endif + std::is_convertible::value, int >::type = 0 > + size_type erase(const KeyType& key) { // this erase only works for objects if (JSON_HEDLEY_LIKELY(is_object()))