Skip to content

Operator= ambiguous in C++1z and GCC 7.1.1 #670

@nick-prat

Description

@nick-prat

With the new GCC 7.1.1 there is a bug with reassigning to strings.

In this code

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

using json = nlohmann::json;

int main() {
    json js;
    std::string name = "";
    name = js["name"];
}

it produces the error

In file included from /usr/include/c++/7.1.1/string:52:0,
                 from json_error.cc:1:
/usr/include/c++/7.1.1/bits/basic_string.h: In instantiation of ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(_Tp) [with _Tp = nlohmann::basic_json<>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_If_sv<_Tp, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&> = std::__cxx11::basic_string<char>&]’:
json_error.cc:10:21:   required from here
/usr/include/c++/7.1.1/bits/basic_string.h:764:28: error: call of overloaded ‘assign(nlohmann::basic_json<>&)’ is ambiguous
  { return this->assign(__sv); }
                            ^
/usr/include/c++/7.1.1/bits/basic_string.h:1313:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       assign(const basic_string& __str)
       ^~~~~~
/usr/include/c++/7.1.1/bits/basic_string.h:1329:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       assign(basic_string&& __str)
       ^~~~~~
/usr/include/c++/7.1.1/bits/basic_string.h:1440:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::assign(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::__sv_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::__sv_type = std::basic_string_view<char>]
       assign(__sv_type __sv)
       ^~~~~~

when built with g++ -std=c++1z filename.cpp but it will compile fine with c++ -std=c++14 filename.cpp

this also occurs if using clang if the GCC libs version is 7.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: please discussplease discuss the issue or vote for your favorite option

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions