Skip to content

Commit b7566c6

Browse files
authored
Harden JSON_HAS_RANGES detection for incomplete C++20 ranges implementations (#5161)
* ✅ add regression test for #4440 Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🐛 exclude breaking libraries Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🐛 exclude breaking libraries Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
1 parent c5b2b26 commit b7566c6

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

include/nlohmann/detail/macro_scope.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@
137137
// ranges header shipping in GCC 11.1.0 (released 2021-04-27) has a syntax error
138138
#if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
139139
#define JSON_HAS_RANGES 0
140+
// libstdc++ < 11 has incomplete C++20 ranges (issue #4440)
141+
#elif defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 11
142+
#define JSON_HAS_RANGES 0
143+
// libc++ < 16 has incomplete C++20 ranges (issue #4440)
144+
#elif defined(__clang__) && !defined(__apple_build_version__) \
145+
&& __clang_major__ < 16 && defined(__GLIBCXX__)
146+
#define JSON_HAS_RANGES 0
147+
#elif defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 160000
148+
#define JSON_HAS_RANGES 0
140149
#elif defined(__cpp_lib_ranges)
141150
#define JSON_HAS_RANGES 1
142151
#else

single_include/nlohmann/json.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,15 @@ JSON_HEDLEY_DIAGNOSTIC_POP
25112511
// ranges header shipping in GCC 11.1.0 (released 2021-04-27) has a syntax error
25122512
#if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
25132513
#define JSON_HAS_RANGES 0
2514+
// libstdc++ < 11 has incomplete C++20 ranges (issue #4440)
2515+
#elif defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 11
2516+
#define JSON_HAS_RANGES 0
2517+
// libc++ < 16 has incomplete C++20 ranges (issue #4440)
2518+
#elif defined(__clang__) && !defined(__apple_build_version__) \
2519+
&& __clang_major__ < 16 && defined(__GLIBCXX__)
2520+
#define JSON_HAS_RANGES 0
2521+
#elif defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 160000
2522+
#define JSON_HAS_RANGES 0
25142523
#elif defined(__cpp_lib_ranges)
25152524
#define JSON_HAS_RANGES 1
25162525
#else

tests/src/unit-regression2.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ using ordered_json = nlohmann::ordered_json;
6666
#endif
6767
#endif
6868

69+
// for #4440
70+
#if JSON_HAS_RANGES == 1
71+
#include <ranges>
72+
#endif
73+
6974
// NLOHMANN_JSON_SERIALIZE_ENUM uses a static std::pair
7075
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
7176
DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors")
@@ -1134,6 +1139,20 @@ TEST_CASE("regression tests 2")
11341139
CHECK(!result.has_value());
11351140
}
11361141
#endif
1142+
1143+
#if JSON_HAS_RANGES == 1
1144+
SECTION("issue #4440 - assert when using std::views::filter and GCC 10")
1145+
{
1146+
auto noOpFilter = std::views::filter([](auto&&) noexcept
1147+
{
1148+
return true;
1149+
});
1150+
json j = {1, 2, 3};
1151+
auto filtered = j | noOpFilter;
1152+
CHECK(*filtered.begin() == 1);
1153+
}
1154+
#endif
1155+
11371156
}
11381157

11391158
TEST_CASE_TEMPLATE("issue #4798 - nlohmann::json::to_msgpack() encode float NaN as double", T, double, float) // NOLINT(readability-math-missing-parentheses, bugprone-throwing-static-initialization)

0 commit comments

Comments
 (0)