-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Open
Labels
Description
Description
Constructing a json object from a C++20 view (e.g. std::ranges::transform_view/std::ranges::filter_view) does not work.
Reproduction steps
Constructing a json object from a std::vector works fine, but constructing from a more basic range such as a vector passed through std::views::filter does not work. This holds for a range of json object as well as a range of plain ints.
Expected vs. actual results
Expected: json value constructs as array with elements from the view (e.g. below: with contents [37, 42, 21]).
Actual: Fails to compile as is_compatible_array_type returns false for some reason.
Minimal code example
#include <iostream>
#include <ranges>
#include <vector>
#include <nlohmann/json.hpp>
int main() {
std::vector<int> nums{1, 2, 37, 42, 21};
auto filteredNums = nums | std::views::filter([](int i) { return i > 10; });
nlohmann::json j(filteredNums);
std::cout << j << std::endl;
}Error messages
<source>:10:20: error: no matching constructor for initialization of 'nlohmann::json' (aka 'basic_json<>')
10 | nlohmann::json j(filteredNums);
| ^ ~~~~~~~~~~~~
[...]
/opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp:20942:5: note: candidate template ignored: requirement 'nlohmann::detail::is_compatible_type<nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char>>, void>, std::ranges::filter_view<std::ranges::ref_view<std::vector<int, std::allocator<int>>>, (lambda at <source>:9:51)>>::value' was not satisfied [with CompatibleType = typename __invoke_result<const std::ranges::views::_Filter &, std::vector<int, std::allocator<int>> &, const (lambda at <source>:9:51) &>::type &, U = detail::uncvref_t<typename __invoke_result<const std::ranges::views::_Filter &, std::vector<int, std::allocator<int>> &, const (lambda at <source>:9:51) &>::type &>]
20942 | basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
| ^
[...]
Compiler and operating system
Clang 22 / GCC 16
Library version
3.11.3, 3.12.0
Validation
- The bug also occurs if the latest version from the
developbranch is used. - I can successfully compile and run the unit tests.