Json: Implicit cast to std::string broken again with VS2019 16.5.0

Created on 24 Mar 2020  路  4Comments  路  Source: nlohmann/json

  • What is the issue you have?
  • What is the expected behavior?
  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

The following program should compile.

#include "nlohmann/json.hpp"

void foo()
{
    std::string s;
    nlohmann::json j;
    s = j;
}
  • And what is the actual behavior instead?
  • Which compiler and operating system are you using? Is it a supported compiler?

With Visual Studio 2019 version 16.5.0, the above fails to compile. This is a recent release, but comes under "Microsoft Visual C++ 2019 / Build Tools 16.3.1+1def00d3d (and possibly later)".

The error message is:

foo.cpp(7,10): error C2593: 'operator =' is ambiguous
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\xstring(2591,19): message : could be 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::initializer_list<_Elem>)'
1>        with
1>        [
1>            _Elem=char
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.25.28610\include\xstring(2492,19): message : or       'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &std::basic_string<char,std::char_traits<char>,std::allocator<char>>::operator =(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&) noexcept(<expr>)'
1>foo.cpp(7,10): message : while trying to match the argument list '(std::string, nlohmann::json)'
  • Did you use a released version of the library or the version from the develop branch?

I tried both a recent release and HEAD of develop.

The same error message is reported when trying to compile test\src\unit-conversions.cpp.

In both cases the error can be resolved by removing the #ifndef _MSC_VER which ends up at https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp#L17468 (and the corresponding #endif!). FWIW, in my testing with VS 2015 (for which that preprocessor condition was added), the code also compiles fine when it is removed.

The compile-time and preprocessor conditions to enable implicit conversions have obviously been revisited many times over the life of nlohmann/json. A good place to start looking at the history is https://github.com/nlohmann/json/pull/188.

bug release item bug fix proposed fix

Most helpful comment

Unfortunately appveyor [1] is still at 16.4.
But nevertheless can you provide a PR based on your findings? It might be easiest to first try dropping the #ifndef altogether.

All 4 comments

Unfortunately appveyor [1] is still at 16.4.
But nevertheless can you provide a PR based on your findings? It might be easiest to first try dropping the #ifndef altogether.

Thanks @garethsb-sony !

With #2008 merged, can this issue be closed?

From my side, yes, my issue is fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edi9999 picture edi9999  路  3Comments

MariaRamos89 picture MariaRamos89  路  4Comments

jmlemetayer picture jmlemetayer  路  3Comments

zkelo picture zkelo  路  3Comments

past-due picture past-due  路  4Comments