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 overload for 'push_back' and 'operator+=' #235

Closed
alestrooisma opened this issue Apr 19, 2016 · 5 comments
Closed

ambiguous overload for 'push_back' and 'operator+=' #235

alestrooisma opened this issue Apr 19, 2016 · 5 comments

Comments

@alestrooisma
Copy link

alestrooisma commented Apr 19, 2016

When compiling this very simple program with g++ 4.9.2, I get "ambiguous overload" errors about both the push_back and the operator+= calls.

#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

int main(int argc, char** argv) {
    json data = {{"key", "value"}};
    data.push_back({"key2", "value2"});
    data += {"key3", "value3"};
    return 0;
}

See here for the compiler output (it's rather large)

@nlohmann
Copy link
Owner

I confirm the error. With clang 7.3.0 (clang-703.0.29), the error is a bit shorter (but basically the same):

issue235.cpp:7:10: error: call to member function 'push_back' is ambiguous
    data.push_back({"key2", "value2"});
    ~~~~~^~~~~~~~~
./../src/json.hpp:4883:10: note: candidate function
    void push_back(basic_json&& val)
         ^
./../src/json.hpp:4919:10: note: candidate function
    void push_back(const basic_json& val)
         ^
./../src/json.hpp:4969:10: note: candidate function
    void push_back(const typename object_t::value_type& val)
         ^
issue235.cpp:8:10: error: use of overloaded operator '+=' is ambiguous (with operand types 'json' (aka 'basic_json<>') and 'void')
    data += {"key3", "value3"};
    ~~~~ ^  ~~~~~~~~~~~~~~~~~~
./../src/json.hpp:4909:15: note: candidate function
    reference operator+=(basic_json&& val)
              ^
./../src/json.hpp:4943:15: note: candidate function
    reference operator+=(const basic_json& val)
              ^
./../src/json.hpp:4993:15: note: candidate function
    reference operator+=(const typename object_t::value_type& val)
              ^

@gregmarr
Copy link
Contributor

I wonder if having an emplace_back that chained to the vector version would be less ambiguous.

@nlohmann
Copy link
Owner

nlohmann commented May 8, 2016

The problem is that {"key", "value"} can be both interpreted as object_t::value_type or std::initializer_list<basic_json>. A solution could be to add an overload void push_back(std::initializer_list<basic_json>) and to create and object_t::value_type from that initializer list. First tries broke some tests, so I am not sure whether this will work in the end.

nlohmann added a commit that referenced this issue May 8, 2016
@nlohmann nlohmann added this to the Release 2.0.0 milestone May 8, 2016
@nlohmann nlohmann self-assigned this May 8, 2016
@nlohmann
Copy link
Owner

nlohmann commented May 8, 2016

Fixed with last commit to develop branch.

@nlohmann nlohmann closed this as completed May 8, 2016
@alestrooisma
Copy link
Author

Can confirm that it is fixed. Thanks for the fix and the great library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants