diff --git a/CHANGELOG.md b/CHANGELOG.md index d140edaa..13224494 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Fixed error `ambiguous overload for 'operator|'` (issue #1411) + v6.17.0 (2020-10-19) ------- diff --git a/extras/tests/MemberProxy/CMakeLists.txt b/extras/tests/MemberProxy/CMakeLists.txt index 6d4c0f6f..9c401ea9 100644 --- a/extras/tests/MemberProxy/CMakeLists.txt +++ b/extras/tests/MemberProxy/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable(MemberProxyTests clear.cpp compare.cpp containsKey.cpp + or.cpp remove.cpp set.cpp size.cpp diff --git a/extras/tests/MemberProxy/or.cpp b/extras/tests/MemberProxy/or.cpp new file mode 100644 index 00000000..ec49bdd7 --- /dev/null +++ b/extras/tests/MemberProxy/or.cpp @@ -0,0 +1,29 @@ +// ArduinoJson - arduinojson.org +// Copyright Benoit Blanchon 2014-2020 +// MIT License + +#include +#include + +using namespace ARDUINOJSON_NAMESPACE; + +TEST_CASE("MemberProxy::operator|()") { + DynamicJsonDocument doc(4096); + + SECTION("const char*") { + doc["a"] = "hello"; + + REQUIRE((doc["a"] | "world") == std::string("hello")); + REQUIRE((doc["b"] | "world") == std::string("world")); + } + + SECTION("Issue #1411") { + doc["sensor"] = "gps"; + + const char *test = "test"; // <- the literal must be captured in a variable + // to trigger the bug + const char *sensor = doc["sensor"] | test; // "gps" + + REQUIRE(sensor == std::string("gps")); + } +} diff --git a/src/ArduinoJson/Variant/VariantOperators.hpp b/src/ArduinoJson/Variant/VariantOperators.hpp index 79379e81..b08d92f4 100644 --- a/src/ArduinoJson/Variant/VariantOperators.hpp +++ b/src/ArduinoJson/Variant/VariantOperators.hpp @@ -20,22 +20,13 @@ struct VariantOperators { // Returns the default value if the VariantRef is undefined of incompatible template friend typename enable_if::value, T>::type operator|( - const TVariant &variant, const T &defaultValue) { + const TVariant &variant, T defaultValue) { if (variant.template is()) return variant.template as(); else return defaultValue; } - // Returns the default value if the VariantRef is undefined of incompatible - // Special case for string: null is treated as undefined - template - friend typename enable_if::value, T>::type operator|( - const TVariant &variant, T defaultValue) { - const char *value = variant.template as(); - return value ? value : defaultValue; - } - // value == TVariant template friend bool operator==(T *lhs, TVariant rhs) {