From 7476f5ee0c6d37b31036cf94992f85560c8e2a73 Mon Sep 17 00:00:00 2001 From: christian Date: Tue, 1 Oct 2019 00:57:27 +0200 Subject: [PATCH] Make json_pointer::back const (resolves #1764) --- include/nlohmann/detail/json_pointer.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- test/src/unit-json_pointer.cpp | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 9076441c..041408a8 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 d27940b8..f7aedd10 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 9746d80d..2fa3033c 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());