nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.97k stars 6.72k forks source link

error: expected initializer before ‘<’ token #3907

Open RerikOp opened 1 year ago

RerikOp commented 1 year ago

Description

When using the single include header (https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp) I get the following error during compilation error: expected initializer before ‘<’ token in line 5323. Sorry if it's an easy fix, I don't have much experience with C++. I'm using g++ together with the latest CUDA compiler nvcc under WSL. Edit: does not work using C++20, C++17 (probably below as well) compiles flawlessly

Reproduction steps

Include "json.hpp" in any header file

Expected vs. actual results

Expected: Successful compilation Actual: No compilation

Minimal code example

No response

Error messages

No response

Compiler and operating system

g++ (Ubuntu 12.1.0-2ubuntu1~22.04) 12.1.0 | Cuda compilation tools, release 12.0, V12.0.76 Build cuda_12.0.r12.0/compiler.31968024_0

Library version

version 3.11.2

Validation

ghost commented 1 year ago

Can confirm the bug for

build-debug/_deps/json-src/include/nlohmann/detail/iterators/iteration_proxy.hpp:241:22: error: expected initializer before ‘<’ token
  241 |     inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;

No error when the library is compiled with c++17

shaoyaoqian commented 11 months ago

Same error. You might compile the program with nvcc as the default compiler. I have the same problem but can't find the solution. I used the 3.8.0 to avoid this problem.

steef435 commented 9 months ago

Looking at this, it seems that support for ranges (a C++20 feature) is disabled on g++11. I have the issue above with nvcc12 and g++10.2.1. Maybe there should be more checks here? (I thought nvcc just used gcc's headers, so maybe it has nothing to do with CUDA/nvcc.)

In any case, I wanted to share the workaround of disabling ranges with

#define JSON_HAS_RANGES 0

before including the library.