From 2df1bc7d4f871d0bb7cc9ca3eccda40247499941 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 23 Nov 2021 10:47:31 +0100 Subject: [PATCH] Fix `call of overloaded 'swap(...)' is ambiguous` (fixes #1678) --- CHANGELOG.md | 1 + .../tests/JsonDocument/BasicJsonDocument.cpp | 6 ++-- extras/tests/JsonDocument/CMakeLists.txt | 1 + extras/tests/JsonDocument/swap.cpp | 13 +++++++++ src/ArduinoJson/MsgPack/endianess.hpp | 21 ++++++++------ src/ArduinoJson/Polyfills/utility.hpp | 28 ------------------- 6 files changed, 31 insertions(+), 39 deletions(-) create mode 100644 extras/tests/JsonDocument/swap.cpp delete mode 100644 src/ArduinoJson/Polyfills/utility.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 7979579e..90759f4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ HEAD * Add safe bool idiom in `JsonString` * Remove `DeserializationError == bool` and `DeserializationError != bool` * Fix `JsonVariant::memoryUsage()` for raw strings +* Fix `call of overloaded 'swap(BasicJsonDocument&, BasicJsonDocument&)' is ambiguous` (issue #1678) v6.18.5 (2021-09-28) ------- diff --git a/extras/tests/JsonDocument/BasicJsonDocument.cpp b/extras/tests/JsonDocument/BasicJsonDocument.cpp index 15b30f9e..a30c8112 100644 --- a/extras/tests/JsonDocument/BasicJsonDocument.cpp +++ b/extras/tests/JsonDocument/BasicJsonDocument.cpp @@ -6,9 +6,9 @@ #include // malloc, free #include #include +#include using ARDUINOJSON_NAMESPACE::addPadding; -using ARDUINOJSON_NAMESPACE::move; class SpyingAllocator { public: @@ -78,7 +78,7 @@ TEST_CASE("BasicJsonDocument") { BasicJsonDocument doc1(4096, log); doc1.set(std::string("The size of this string is 32!!")); - BasicJsonDocument doc2(move(doc1)); + BasicJsonDocument doc2(std::move(doc1)); REQUIRE(doc2.as() == "The size of this string is 32!!"); REQUIRE(doc1.as() == "null"); @@ -111,7 +111,7 @@ TEST_CASE("BasicJsonDocument") { doc1.set(std::string("The size of this string is 32!!")); BasicJsonDocument doc2(8, log); - doc2 = move(doc1); + doc2 = std::move(doc1); REQUIRE(doc2.as() == "The size of this string is 32!!"); REQUIRE(doc1.as() == "null"); diff --git a/extras/tests/JsonDocument/CMakeLists.txt b/extras/tests/JsonDocument/CMakeLists.txt index 18c07f0b..612457de 100644 --- a/extras/tests/JsonDocument/CMakeLists.txt +++ b/extras/tests/JsonDocument/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(JsonDocumentTests size.cpp StaticJsonDocument.cpp subscript.cpp + swap.cpp ) add_test(JsonDocument JsonDocumentTests) diff --git a/extras/tests/JsonDocument/swap.cpp b/extras/tests/JsonDocument/swap.cpp new file mode 100644 index 00000000..85be575b --- /dev/null +++ b/extras/tests/JsonDocument/swap.cpp @@ -0,0 +1,13 @@ +#include + +#include +#include + +using namespace std; + +TEST_CASE("std::swap") { + SECTION("DynamicJsonDocument*") { + DynamicJsonDocument *p1, *p2; + swap(p1, p2); // issue #1678 + } +} diff --git a/src/ArduinoJson/MsgPack/endianess.hpp b/src/ArduinoJson/MsgPack/endianess.hpp index 74f7e9da..669c8e9d 100644 --- a/src/ArduinoJson/MsgPack/endianess.hpp +++ b/src/ArduinoJson/MsgPack/endianess.hpp @@ -5,25 +5,30 @@ #pragma once #include -#include namespace ARDUINOJSON_NAMESPACE { #if ARDUINOJSON_LITTLE_ENDIAN +inline void swapBytes(uint8_t &a, uint8_t &b) { + uint8_t t(a); + a = b; + b = t; +} + inline void fixEndianess(uint8_t *p, integral_constant) { - swap(p[0], p[7]); - swap(p[1], p[6]); - swap(p[2], p[5]); - swap(p[3], p[4]); + swapBytes(p[0], p[7]); + swapBytes(p[1], p[6]); + swapBytes(p[2], p[5]); + swapBytes(p[3], p[4]); } inline void fixEndianess(uint8_t *p, integral_constant) { - swap(p[0], p[3]); - swap(p[1], p[2]); + swapBytes(p[0], p[3]); + swapBytes(p[1], p[2]); } inline void fixEndianess(uint8_t *p, integral_constant) { - swap(p[0], p[1]); + swapBytes(p[0], p[1]); } inline void fixEndianess(uint8_t *, integral_constant) {} diff --git a/src/ArduinoJson/Polyfills/utility.hpp b/src/ArduinoJson/Polyfills/utility.hpp deleted file mode 100644 index c99bc991..00000000 --- a/src/ArduinoJson/Polyfills/utility.hpp +++ /dev/null @@ -1,28 +0,0 @@ -// ArduinoJson - https://arduinojson.org -// Copyright Benoit Blanchon 2014-2021 -// MIT License - -#pragma once - -#include "type_traits.hpp" - -namespace ARDUINOJSON_NAMESPACE { -template -inline void swap(T& a, T& b) { - T t(a); - a = b; - b = t; -} - -#if ARDUINOJSON_HAS_RVALUE_REFERENCES -template -typename remove_reference::type&& move(T&& t) { - return static_cast::type&&>(t); -} -#else -template -T& move(T& t) { - return t; -} -#endif -} // namespace ARDUINOJSON_NAMESPACE