Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambiguous function call to get with pointer type and constant json object in VS2015 (15.4.4) #852

Closed
MaxSavenkov opened this issue Dec 2, 2017 · 2 comments
Labels
platform: visual studio related to MSVC solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@MaxSavenkov
Copy link

This code will not compile in Visual Studio 2015 (15.4.4) with the latest nlohmann::json code:

    const nlohmann::json test;
    const auto *s = test.get<const std::string*>();

The error message is:

1>F:\rolecraft2\prj\rolecraft\ui\ui_view_character.cpp(13): error C2668: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::get': ambiguous call to overloaded function
1>F:\rolecraft2\ext\nlohmann\json.hpp(9669): note: could be 'const PointerType nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::get<const std::string*,0>(void) noexcept const'
1>        with
1>        [
1>            PointerType=const std::string *
1>        ]
1>F:\rolecraft2\ext\nlohmann\json.hpp(9657): note: or       'PointerType nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::get<const std::string*,0>(void) noexcept'
1>        with
1>        [
1>            PointerType=const std::string *
1>        ]
1>F:\rolecraft2\ext\nlohmann\json.hpp(9567): note: or       'ValueType nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::get<const std::string*,const std::string*,0>(void) noexcept(<expr>) const'
1>        with
1>        [
1>            ValueType=const std::string *
1>        ]

Seems like Visual Studio's compiler can't decide whether to use ValueType version of get(), or PointerType one. The issue is easily worked around by using get_ptr() instead, but I still would like to report it, as it was working in older versions of the library (2.0.5, for example).

The issue can be fixed by adding

            not std::is_pointer<ValueType>::value and

to enable_if template of ValueType get() const noexcept method.

@nlohmann nlohmann added the platform: visual studio related to MSVC label Dec 3, 2017
@theodelrieu
Copy link
Contributor

Thanks for the report.

Yes, it worked before 2.10, until user defined conversions were added.

There has been some thoughts about whether or not we should allow user defined conversions for pointers.

Another option would be to delegate some calls to get_ptr (the same thing goes for reference arguments), which should work well unless we add support for pointer conversions. In that case there would be some tricky corner cases with basic types like basic_json::string_t.

TL;DR

Status quo:

  • Pointer/reference not supported with get
  • Use get_ptr/get_ref on primitive types.

@MaxSavenkov
Copy link
Author

OK, thanks, using get_ptr works for me.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Dec 5, 2017
@nlohmann nlohmann closed this as completed Dec 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: visual studio related to MSVC solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants