jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

ambiguous overload for 'operator[]' of Json::Value' when compiled for x64

Open dhMat opened this issue 4 years ago • 3 comments

Describe the bug When switching from x86 to x64 I run into following compiler error:

error: ambiguous overload for 'operator[]' (operand types are 'const Json::Value' and 'size_t' {aka 'long long unsigned int'})
     const Json::Value& jsLoop = jsLoopList[loop];
                                           ^
In file included from Model.cc:3:
..\include/json/json.h:977:16: note: candidate: 'const Json::Value& Json::Value::operator[](Json::Value::ArrayIndex) const'
   const Value& operator[](ArrayIndex index) const;
                ^~~~~~~~
..\include/json/json.h:978:16: note: candidate: 'const Json::Value& Json::Value::operator[](int) const'
   const Value& operator[](int index) const;

It is triggered for example by following code snippet:

Json::Value jsLoopList;
// initialize jsLoopList as array

for(size_t loop = 0; loop < loopCount; ++loop) {
    const Json::Value& jsLoop = jsLoopList[loop]; // works for x86, fails for x64
    // do some stuff ...
}

The problem is that with the change from 32 to 64 bit the type of size_t changed and now the compiler does not find an exact match. The solution is to switch to int, but this have the drawback that negative values are possible. The alternative is to use Json::ArrayIndex.

Still it would be great if the array operator of Json::Value can be used like for container classes from the standard lib, e.g. for std::vector.

To Reproduce Steps to reproduce the behavior:

  1. Compile code snippet for x64

Expected behavior Same behaviour as for x86: The code compiles and works.

Desktop:

  • OS: Windows
  • MinGW: g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
  • Tested with jsoncpp 0.9.5 and 0.10.0

dhMat avatar Dec 23 '21 14:12 dhMat

Hello, i also find this bug, i write this code to fix it:

` Json::Value jsLoopList; // initialize jsLoopList as array

for(auto item : jsLoopList) { const Json::Value& jsLoop = item; // do some stuff ... } `

ejoful avatar Sep 14 '22 09:09 ejoful

Seems that the x64 issue is still there when using Visual Studio 2022.

zwm88 avatar Jan 20 '23 20:01 zwm88

I have the same issue with Visual Studio 2022.

Noremos avatar Mar 03 '23 06:03 Noremos