diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 74f00b6c7..228084a5a 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -845,28 +845,17 @@ private: } public: - void destroy_string() noexcept + template + static void destroy_pointer(T*& ptr) noexcept { - if (string == nullptr) + if (ptr == nullptr) { // not initialized (e.g., due to exception in the ctor) return; } - AllocatorType alloc; - std::allocator_traits::destroy(alloc, string); - std::allocator_traits::deallocate(alloc, string, 1); - } - - void destroy_binary() noexcept - { - if (binary == nullptr) - { - // not initialized (e.g., due to exception in the ctor) - return; - } - AllocatorType alloc; - std::allocator_traits::destroy(alloc, binary); - std::allocator_traits::deallocate(alloc, binary, 1); + AllocatorType alloc; + std::allocator_traits::destroy(alloc, ptr); + std::allocator_traits::deallocate(alloc, ptr, 1); } // t must be value_t::array or value_t::object @@ -1032,11 +1021,11 @@ public: switch (t) { case value_t::string: - destroy_string(); + destroy_pointer(string); break; case value_t::binary: - destroy_binary(); + destroy_pointer(binary); break; case value_t::object: