From 165f91ed3c9fb8f3e4a912da7338452575c9c4a1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 25 Oct 2026 12:04:43 +0100 Subject: [PATCH] Build the DOM parser with Visual Studio 2015 again Since #5882, the three VS 2015 jobs of AppVeyor fail on develop: - the detection of keys that can be indexed instantiated std::hash for every string type, which VS 2015 rejects with a static_assert for a type it does not hash (alt_string): std::hash is now looked at only for objects in a vector - the frame stack copied its frames with std::copy to a raw pointer, which VS 2015 reports as unsafe (C4996, an error with /WX): a loop now Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/json_sax.hpp | 19 +++++++++++++++---- single_include/nlohmann/json.hpp | 19 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 6fd0be52d..676b36290 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -650,9 +650,16 @@ class json_sax_dom_parser : public json_sax_dom_error_state /// so that a big object can find the keys it has in a hash table instead of /// searching all of them template - using detect_indexable_keys = decltype(std::declval().emplace_new_key(std::declval(), std::declval()), - std::hash()(std::declval())); - static constexpr bool indexed_keys = is_detected::value; + using detect_emplace_new_key = decltype(std::declval().emplace_new_key(std::declval(), std::declval())); + template + using detect_hash = decltype(std::hash()(std::declval())); + /// (std::hash is only looked at for objects in a vector: the one of VS 2015 + /// fails to compile, instead of being undetectable, for a type it does not hash) + template + struct has_indexable_keys : std::false_type {}; + template + struct has_indexable_keys : is_detected {}; + static constexpr bool indexed_keys = has_indexable_keys::value, string_t>::value; /// what a frame holds for objects that are kept in a vector: the key index /// of an object with many keys, and for an object that is collected on the @@ -774,7 +781,11 @@ class json_sax_dom_parser : public json_sax_dom_error_state { const std::size_t new_capacity = capacity * 2; std::unique_ptr new_heap(new frame[new_capacity]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - std::copy(data, data + count, new_heap.get()); + // (a loop, not std::copy: VS 2015 rejects std::copy to a raw pointer, C4996) + for (std::size_t i = 0; i < count; ++i) + { + new_heap[i] = data[i]; + } heap = std::move(new_heap); data = heap.get(); capacity = new_capacity; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a9e40693d..fc4d96ccb 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15773,9 +15773,16 @@ class json_sax_dom_parser : public json_sax_dom_error_state /// so that a big object can find the keys it has in a hash table instead of /// searching all of them template - using detect_indexable_keys = decltype(std::declval().emplace_new_key(std::declval(), std::declval()), - std::hash()(std::declval())); - static constexpr bool indexed_keys = is_detected::value; + using detect_emplace_new_key = decltype(std::declval().emplace_new_key(std::declval(), std::declval())); + template + using detect_hash = decltype(std::hash()(std::declval())); + /// (std::hash is only looked at for objects in a vector: the one of VS 2015 + /// fails to compile, instead of being undetectable, for a type it does not hash) + template + struct has_indexable_keys : std::false_type {}; + template + struct has_indexable_keys : is_detected {}; + static constexpr bool indexed_keys = has_indexable_keys::value, string_t>::value; /// what a frame holds for objects that are kept in a vector: the key index /// of an object with many keys, and for an object that is collected on the @@ -15897,7 +15904,11 @@ class json_sax_dom_parser : public json_sax_dom_error_state { const std::size_t new_capacity = capacity * 2; std::unique_ptr new_heap(new frame[new_capacity]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - std::copy(data, data + count, new_heap.get()); + // (a loop, not std::copy: VS 2015 rejects std::copy to a raw pointer, C4996) + for (std::size_t i = 0; i < count; ++i) + { + new_heap[i] = data[i]; + } heap = std::move(new_heap); data = heap.get(); capacity = new_capacity;