diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 9076441c8..041408a80 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -267,7 +267,7 @@ class json_pointer @since version 3.6.0 */ - const std::string& back() + const std::string& back() const { if (JSON_HEDLEY_UNLIKELY(empty())) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index d27940b85..f7aedd102 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10232,7 +10232,7 @@ class json_pointer @since version 3.6.0 */ - const std::string& back() + const std::string& back() const { if (JSON_HEDLEY_UNLIKELY(empty())) { diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index 9746d80d3..2fa3033cd 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -559,6 +559,10 @@ TEST_CASE("JSON pointers") CHECK(!ptr.empty()); CHECK(j[ptr] == j["answer"]["everything"]); + // check access via const pointer + const auto cptr = ptr; + CHECK(cptr.back() == "everything"); + ptr.pop_back(); ptr.pop_back(); CHECK(ptr.empty());