Json: compiling with gcc 7 -> error on bool operator <

Created on 21 May 2017  Â·  22Comments  Â·  Source: nlohmann/json

when compiling i get

json.hpp:6395:66: error: wrong number of template arguments (1, should be 2) return *lhs.m_value.array < *rhs.m_value.array;

and

/usr/include/c++/7/array:94:12: note: provided for ‘template<class _Tp, long unsigned int _Nm> struct std::array’ struct array

proposed fix

Most helpful comment

Changing the line in json.hpp to read

return (*lhs.m_value.array) < *rhs.m_value.array;

seems to fix the error using that simple test case. It's quite possible this is a GCC bug; I can't understand why .array< should be interpreted as template parameters to array with that . preceding it. Furthermore, clang compiles the snippet without issue in C++11 and in C++14 mode.

All 22 comments

Could you provide more information: what do you compile? Are there more error messages with other file locations?

here is the full output of the compilation

kostas@kostaslinuxlaptop:~/Code/carnd/CarND-MPC/build$ cmake .. && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/kostas/Code/carnd/CarND-MPC/build
Scanning dependencies of target mpc
[ 33%] Building CXX object CMakeFiles/mpc.dir/src/MPC.cpp.o
[ 66%] Building CXX object CMakeFiles/mpc.dir/src/main.cpp.o
In file included from /home/kostas/Code/carnd/CarND-MPC/src/main.cpp:10:0:
/home/kostas/Code/carnd/CarND-MPC/src/json.hpp: In function ‘bool nlohmann::operator<(nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference, nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference)’:
/home/kostas/Code/carnd/CarND-MPC/src/json.hpp:6395:66: error: wrong number of template arguments (1, should be 2)
                         return *lhs.m_value.array < *rhs.m_value.array;
                                                                  ^~~~~
In file included from /usr/include/c++/7/tuple:39:0,
                 from /usr/include/c++/7/mutex:38,
                 from /usr/include/uWS/Epoll.h:11,
                 from /usr/include/uWS/Backend.h:12,
                 from /usr/include/uWS/Networking.h:69,
                 from /usr/include/uWS/WebSocketProtocol.h:5,
                 from /usr/include/uWS/WebSocket.h:4,
                 from /usr/include/uWS/Group.h:4,
                 from /usr/include/uWS/Hub.h:4,
                 from /usr/include/uWS/uWS.h:4,
                 from /home/kostas/Code/carnd/CarND-MPC/src/main.cpp:2:
/usr/include/c++/7/array:94:12: note: provided for ‘template<class _Tp, long unsigned int _Nm> struct std::array’
     struct array
            ^~~~~
CMakeFiles/mpc.dir/build.make:86: recipe for target 'CMakeFiles/mpc.dir/src/main.cpp.o' failed
make[2]: *** [CMakeFiles/mpc.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mpc.dir/all' failed
make[1]: *** [CMakeFiles/mpc.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Could you paste the complete error message? I compiled the test suite of this library with GCC7 without problems.

I just did above :)

It seems that the compiler tries to parse array as std::array. I can't install all the dependencies right now - can you try to create a smaller example with fewer dependencies?

To rule out a problem with your compiler: can you try to compile the unit tests for nlohmann/json (by just calling cmake/make after cloning nlohmann/json?

I tried cmake -DCMAKE_C_COMPILER="/usr/bin/gcc-6" -DCMAKE_CXX_COMPILER="/usr/bin/g++-6" .. && make and it builds fine
So its definitely an issue with the compiler. Probably due to dependencies.

So GCC6 works with your code, but GCC7 does not? This is strange. Then again, can you reproduce the error which fewer dependencies?

I will.

I met the same problem, here's one way to produce it:

a.cc:

#include <string>
using namespace std;

#include "json.hpp"

Compiling it with g++ -c a.cc will produce the error.

I cannot reproduce this issue with

  • g++-7 (Homebrew GCC 7.1.0) 7.1.0
  • g++ (GCC) 8.0.0 20170601 (experimental)

I can compile and run the complete test suite from the develop branch.

@whenov What is the OS/version of your compiler, and can you provide the error message?

I'm having the exact same issue when compiling any OpenFrameworks app on Arch Linux since updating gcc to 7.1.1

/home/thinkpad2000/Downloads/of_v20170517_linux64_release/libs/json/include/json.hpp:5002:62: error: wrong number of template arguments (1, should be 2)
                   return *lhs.m_value.array < *rhs.m_value.array;
                                                            ^~~~~
In file included from /usr/include/c++/7.1.1/tuple:39:0,
               from /usr/include/c++/7.1.1/bits/unique_ptr.h:37,
               from /usr/include/c++/7.1.1/bits/locale_conv.h:41,
               from /usr/include/c++/7.1.1/locale:43,
               from /usr/include/c++/7.1.1/iomanip:43
...

I tried to help @alberto2000 track this down in IRC earlier. In https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/utils/ofConstants.h#L435 -- 'using namespace std;' is present and indirectly ahead of the include for json.hpp, which seems to trigger the issue.

@buffos -- your code also has a 'using namespace std;' indirectly before json.hpp is included

@nlohmann -- I did the same as @whenov and had the same error message about template arguments, which goes away if 'using namespace std;' is commented out. The test suite built and ran fine.

@nlohmann
OS: Arch Linux
Compiler: gcc version 7.1.1 20170516 (GCC)
Error message:

json.hpp: In function ‘bool nlohmann::operator<(nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference, nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference)’:
json.hpp:6057:62: error: wrong number of template arguments (1, should be 2)
                     return *lhs.m_value.array < *rhs.m_value.array;
                                                              ^~~~~
In file included from json.hpp:33:0,
                 from a.cc:4:
/usr/include/c++/7.1.1/array:94:12: note: provided for ‘template<class _Tp, long unsigned int _Nm> struct std::array’
     struct array
            ^~~~~

Ok, now I can also reproduce the error with the following code and GCC-7:

using namespace std;
#include <json.hpp>

int main()
{}

The error is

In file included from std.cpp:2:0:
src/json.hpp: In function 'bool nlohmann::operator<(nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference, nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference)':
src/json.hpp:6384:62: error: wrong number of template arguments (1, should be 2)
                     return *lhs.m_value.array < *rhs.m_value.array;
                                                              ^~~~~
In file included from src/json.hpp:33:0,
                 from std.cpp:2:
/usr/local/Cellar/gcc/7.1.0/include/c++/7.1.0/array:94:12: note: provided for 'template<class _Tp, long unsigned int _Nm> struct std::array'
     struct array
            ^~~~~
make: *** [std] Error 1

It is very strange that the name array is mistaken as std::array. I wonder if this is a bug in GCC.

Changing the line in json.hpp to read

return (*lhs.m_value.array) < *rhs.m_value.array;

seems to fix the error using that simple test case. It's quite possible this is a GCC bug; I can't understand why .array< should be interpreted as template parameters to array with that . preceding it. Furthermore, clang compiles the snippet without issue in C++11 and in C++14 mode.

Yes, definitely seems like a compiler bug, especially if it worked fine in other GCC7 versions, then it's probably a regression.

@jebrosen Thanks for the fix. I shall implement it. I further try to get a GCC7 working in Travis so such errors can be detected automatically.

As for the bug - it would be great to boil the problem down to a size that is "digestible" by StackOverflow. Any help would be greatly appreciated.

I opened an issue on StackOverflow, see https://stackoverflow.com/questions/44367859/using-stdarray-and-using-array-as-name

It is not sure yet whether this is an issue in the language or the compilers. Nevertheless, adding parentheses fixes the problem, so I close this issue.

This issue seems to have come back with the 3.6.0 release. I guess this was caused by some automatic reformatting: Here's the line that previously had parenthesis

https://github.com/nlohmann/json/blob/release/3.6.0/include/nlohmann/json.hpp#L5731

Also this problem still appears with GCC 8.2.1

Oh, that's a pity! I use GCC 7 and 8 in the CI, but did not catch this issue. Is it just the array comparison?

@nlohmann I haven't tested the commit you linked but adding the parenthesis by hand locally fixes it for me.

Was this page helpful?
0 / 5 - 0 ratings