From 21ff4a2ad257c2e9bb12a54fd149fe0c03ccdf2d Mon Sep 17 00:00:00 2001 From: Xav83 Date: Thu, 19 Sep 2019 17:58:41 +0200 Subject: [PATCH] Correct a warning from cppcheck: binary_writer.hpp:869: (style) Consider using std::accumulate algorithm instead of a raw loop. https://github.com/Xav83/nlohmann-json-cppcheck/commit/910a7d2b873dd7ae92ec81cced2bf73200ff4848/checks#step:5:107 Signed-off-by: Xav83 --- include/nlohmann/detail/output/binary_writer.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 29093dd372..fd0b5a7d08 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -861,13 +861,10 @@ class binary_writer */ static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value) { - std::size_t embedded_document_size = 0ul; - std::size_t array_index = 0ul; - - for (const auto& el : value) + const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), [array_index = 0ul](std::size_t result, const auto & el) mutable { - embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el); - } + return result + calc_bson_element_size(std::to_string(array_index++), el); + }); return sizeof(std::int32_t) + embedded_document_size + 1ul; }