Skip to content

Commit

Permalink
Use SFNIAE in JsonArray
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Aug 9, 2023
1 parent 1346f78 commit 2aba467
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/ArduinoJson/Array/JsonArray.hpp
Expand Up @@ -45,10 +45,21 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
// Returns a reference to the new element.
// https://arduinojson.org/v6/api/jsonarray/add/
template <typename T>
JsonVariant add() const {
typename detail::enable_if<!detail::is_same<T, JsonVariant>::value, T>::type
add() const {
return add<JsonVariant>().to<T>();
}

// Appends a new (null) element to the array.
// Returns a reference to the new element.
// https://arduinojson.org/v6/api/jsonarray/add/
template <typename T>
typename detail::enable_if<detail::is_same<T, JsonVariant>::value, T>::type
add() const {
return JsonVariant(detail::ArrayData::addElement(data_, resources_),
resources_);
}

// Appends a value to the array.
// https://arduinojson.org/v6/api/jsonarray/add/
template <typename T>
Expand Down Expand Up @@ -174,12 +185,6 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
detail::ResourceManager* resources_;
};

template <>
inline JsonVariant JsonArray::add<JsonVariant>() const {
return JsonVariant(detail::ArrayData::addElement(data_, resources_),
resources_);
}

ARDUINOJSON_END_PUBLIC_NAMESPACE

ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoJson/Variant/VariantRefBase.hpp
Expand Up @@ -144,7 +144,7 @@ class VariantRefBase : public VariantTag {
return add<JsonVariant>().template to<T>();
}

// Appends a new (empty) element to the array.
// Appends a new (null) element to the array.
// Returns a reference to the new element.
// https://arduinojson.org/v6/api/jsonvariant/add/
template <typename T>
Expand Down

0 comments on commit 2aba467

Please sign in to comment.