From f28157cab76a1150f62522185d7b0df49de5312f Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sun, 13 Nov 2016 20:19:36 +0100 Subject: [PATCH] Fixed compilation error when index is not an `int` (issue #381) --- CHANGELOG.md | 1 + include/ArduinoJson/Internals/StringFuncs.hpp | 6 ++++++ include/ArduinoJson/JsonObjectSubscript.hpp | 7 ------- include/ArduinoJson/JsonVariantBase.hpp | 8 ++++++-- test/JsonVariant_Subscript_Tests.cpp | 5 ++++- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1689b4ff..f3a9193d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ HEAD ---- * Added support for PROGMEM (issue #76) +* Fixed compilation error when index is not an `int` (issue #381) v5.7.0 ------ diff --git a/include/ArduinoJson/Internals/StringFuncs.hpp b/include/ArduinoJson/Internals/StringFuncs.hpp index cb6b5cb1..91404d4d 100644 --- a/include/ArduinoJson/Internals/StringFuncs.hpp +++ b/include/ArduinoJson/Internals/StringFuncs.hpp @@ -44,6 +44,7 @@ struct CharPtrFuncs { } static const bool has_append = false; + static const bool has_equals = true; static const bool should_duplicate = false; }; @@ -56,6 +57,9 @@ struct StringFuncs : CharPtrFuncs {}; template struct StringFuncs : CharPtrFuncs {}; +template +struct StringFuncs : CharPtrFuncs {}; + template struct StdStringFuncs { template @@ -76,6 +80,7 @@ struct StdStringFuncs { } static const bool has_append = true; + static const bool has_equals = true; static const bool should_duplicate = true; }; @@ -108,6 +113,7 @@ struct StringFuncs { } static const bool has_append = false; + static const bool has_equals = true; static const bool should_duplicate = true; }; #endif diff --git a/include/ArduinoJson/JsonObjectSubscript.hpp b/include/ArduinoJson/JsonObjectSubscript.hpp index b56a2212..a5164b25 100644 --- a/include/ArduinoJson/JsonObjectSubscript.hpp +++ b/include/ArduinoJson/JsonObjectSubscript.hpp @@ -84,13 +84,6 @@ template inline JsonObjectSubscript JsonObject::operator[](const TString& key) { return JsonObjectSubscript(*this, key); } - -template -template -inline const JsonObjectSubscript JsonVariantBase::operator[]( - const TString& key) const { - return asObject()[key]; -} } // namespace ArduinoJson #ifdef _MSC_VER diff --git a/include/ArduinoJson/JsonVariantBase.hpp b/include/ArduinoJson/JsonVariantBase.hpp index 11c678d7..13875a94 100644 --- a/include/ArduinoJson/JsonVariantBase.hpp +++ b/include/ArduinoJson/JsonVariantBase.hpp @@ -77,8 +77,12 @@ class JsonVariantBase : public Internals::JsonPrintable { // an object. // Return JsonVariant::invalid() if the variant is not an object. template - FORCE_INLINE const JsonObjectSubscript operator[]( - const TString &key) const; + FORCE_INLINE + typename TypeTraits::EnableIf::has_equals, + const JsonObjectSubscript >::type + operator[](const TString &key) const { + return asObject()[key]; + } private: const TImpl *impl() const { diff --git a/test/JsonVariant_Subscript_Tests.cpp b/test/JsonVariant_Subscript_Tests.cpp index 069b95f9..c6b5c4fa 100644 --- a/test/JsonVariant_Subscript_Tests.cpp +++ b/test/JsonVariant_Subscript_Tests.cpp @@ -5,8 +5,8 @@ // https://github.com/bblanchon/ArduinoJson // If you like this project, please add a star! -#include #include +#include class JsonVariant_Subscript_Tests : public ::testing::Test { protected: @@ -24,6 +24,9 @@ TEST_F(JsonVariant_Subscript_Tests, Array) { EXPECT_EQ(2, _variant.size()); EXPECT_STREQ("element at index 0", _variant[0].asString()); EXPECT_STREQ("element at index 1", _variant[1].asString()); + EXPECT_STREQ( + "element at index 0", + _variant[static_cast(0)].asString()); // issue #381 EXPECT_FALSE(_variant[-1].success()); EXPECT_FALSE(_variant[3].success()); EXPECT_FALSE(_variant["0"].success());