Skip to content

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

Closed

Description

@alestrooisma

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)

Activity

  1. nlohmann commented on Apr 19, 2016

    @nlohmann
    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)
                  ^
    
  2. gregmarr commented on Apr 19, 2016

    @gregmarr
    Contributor

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

  3. nlohmann commented on May 8, 2016

    @nlohmann
    Owner

    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.

  4. added a commit that references this issue on May 8, 2016
  5. added this to the Release 2.0.0 milestone on May 8, 2016
  6. self-assigned this
    on May 8, 2016
  7. nlohmann commented on May 8, 2016

    @nlohmann
    Owner

    Fixed with last commit to develop branch.

  8. alestrooisma commented on May 9, 2016

    @alestrooisma
    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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions