Skip to content

Commit 48ee4a1

Browse files
committed
Use a const reference in is_convertible()
1 parent cd4b2b2 commit 48ee4a1

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

extras/tests/Misc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_executable(MiscTests
66
arithmeticCompare.cpp
77
conflicts.cpp
88
issue1967.cpp
9+
issue2129.cpp
910
JsonString.cpp
1011
NoArduinoHeader.cpp
1112
printable.cpp

extras/tests/Misc/issue2129.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ArduinoJson - https://arduinojson.org
2+
// Copyright © 2014-2024, Benoit BLANCHON
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
template <typename T>
9+
class Nullable {
10+
public:
11+
Nullable() : value_{} {}
12+
Nullable(T value) : value_{value} {}
13+
14+
operator T() const {
15+
return value_;
16+
}
17+
18+
operator T&() {
19+
return value_;
20+
}
21+
22+
bool is_valid() const {
23+
return value_ != invalid_value_;
24+
}
25+
26+
T value() const {
27+
return value_;
28+
}
29+
30+
private:
31+
T value_;
32+
static T invalid_value_;
33+
};
34+
35+
template <>
36+
float Nullable<float>::invalid_value_ = std::numeric_limits<float>::lowest();
37+
38+
template <typename T>
39+
void convertToJson(const Nullable<T>& src, JsonVariant dst) {
40+
if (src.is_valid()) {
41+
dst.set(src.value());
42+
} else {
43+
dst.clear();
44+
}
45+
}
46+
47+
TEST_CASE("Issue #2129") {
48+
Nullable<float> nullable_value = Nullable<float>{123.4f};
49+
50+
JsonDocument doc;
51+
52+
doc["value"] = nullable_value;
53+
54+
REQUIRE(doc["value"].as<float>() == Approx(123.4f));
55+
}

src/ArduinoJson/Polyfills/type_traits/is_convertible.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct is_convertible {
2727
static int probe(To);
2828
static char probe(...);
2929

30-
static From& from_;
30+
static const From& from_;
3131

3232
public:
3333
static const bool value = sizeof(probe(from_)) == sizeof(int);

0 commit comments

Comments
 (0)