-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Open
Labels
Description
Description
When playing with current develop branch I noticed that part of my code no longer compiles. An example for reproduction follows below.
Tested on Windows with both MSVC (VS2022) and clang-cl, compiled with C++20 defined.
The fix seems easy, simply changing the return statement to:
...
return std::optional<json>(valRoot.at(CFG_PROP_DEFAULT));
...The code compiled fine in v3.12.0, so I guess it may be related to recent changes, maybe in #4742?
Not sure if this is a bug, or intended behavior.
Reproduction steps
Build the sample code.
Expected vs. actual results
The code should compile?
Minimal code example
#include <nlohmann/json.hpp>
#include <optional>
using json = nlohmann::json;
inline constexpr const char* CFG_PROP_DEFAULT = "default";
std::optional<json> GetValue(const json& valRoot)
{
if (valRoot.contains(CFG_PROP_DEFAULT)) {
return valRoot.at(CFG_PROP_DEFAULT);
}
return std::nullopt;
}
int main(int argc, char* argv[])
{
json jval{};
auto result = GetValue(jval);
return 0;
}Error messages
>------ Build All started: Project: json2_test, Configuration: x64_Debug_xClang-x64_Debug_xClang ------
[1/2] Building CXX object CMakeFiles\json2_test.dir\json2_test.cpp.obj
FAILED: CMakeFiles/json2_test.dir/json2_test.cpp.obj
D:\Dev\LLVM\bin\clang-cl.exe /nologo -TP -imsvcD:\Work_OSS\_risa2000\nlohmann_json\include --target=amd64-pc-windows-msvc -fdiagnostics-absolute-paths /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -std:c++20 -MDd /showIncludes /FoCMakeFiles\json2_test.dir\json2_test.cpp.obj /FdCMakeFiles\json2_test.dir\ -c -- D:\Work\vsprojects\_tests\json2_test\json2_test.cpp
D:\Work\vsprojects\_tests\json2_test\json2_test.cpp(11,16): error : conversion from 'const value_type' (aka 'const nlohmann::basic_json<>') to 'std::optional<json>' (aka 'optional<basic_json<>>') is ambiguous
11 | return valRoot.at(CFG_PROP_DEFAULT);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\include\optional(270,54): note: candidate constructor [with _Ty2 = const nlohmann::basic_json<> &, $1 = 0]
270 | constexpr explicit(!is_convertible_v<_Ty2, _Ty>) optional(_Ty2&& _Right)
| ^
D:\Work_OSS\_risa2000\nlohmann_json\include\nlohmann\json.hpp(1924,55): note: candidate function [with ValueType = std::optional<nlohmann::basic_json<>>, $1 = 0]
1924 | JSON_EXPLICIT operator ValueType() const
| ^
1 error generated.
ninja: build stopped: subcommand failed.
Build All failed.Compiler and operating system
Windows, compilers Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35222 for x64 (VS2022), clang version 21.1.8
Library version
develop
Validation
- The bug also occurs if the latest version from the
developbranch is used. - I can successfully compile and run the unit tests.