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

explicit conversion to string not considered for std::map keys in GCC8 #2096

Closed
ottojo opened this issue May 9, 2020 · 3 comments
Closed
Labels
kind: bug state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@ottojo
Copy link

ottojo commented May 9, 2020

  • What is the issue you have?
    Compilation fails with GCC8 when trying to serialize a std::map whose Key-type provides explicit conversion to std::string

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

#include <iostream>
#include <nlohmann/json.hpp>

class ID {
public:
    explicit ID(int i) : id(i) {};

    explicit operator std::string() const {
        return std::to_string(id);
    }

    bool operator<(const ID &rhs) const {
        return id < rhs.id;
    }

private:
    int id;
};

int main() {
    std::map<ID, std::string> map = {
            {ID(7),  "test1"},
            {ID(13), "test2"}
    };

    nlohmann::json mapJson = map;
    std::cout << mapJson.dump(4) << std::endl;
 }
  • What is the expected behavior?
    Serialization to
{
    "13": "test2",
    "7": "test1"
}
  • And what is the actual behavior instead?
    Does not compile when using gcc version 8
g++-8 (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0

Compiler error:

no known conversion for argument 2 from ‘std::pair<const ID, std::__cxx11::basic_string<char> >::first_type’ {aka ‘const ID’} to ‘const key_type&’ {aka ‘const std::__cxx11::basic_string<char>&’}
  • Which compiler and operating system are you using? Is it a supported compiler?

    • I found this issue only when compiling with GCC 8.4.0
    • Other compilers tested:
      • g++-9 (Ubuntu 9.3.0-11ubuntu0~18.04.1) 9.3.0
      • g++-10 (Ubuntu 10-20200416-0ubuntu1~18.04) 10.0.1 20200416 (experimental) [master revision 3c3f12e2a76:dcee354ce56:44b326839d864fc10c459916abcc97f35a9ac3de]
      • clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
      • clang version 11.0.0-++20200506091409+ca09dab303f-1~exp1~20200506072011.1666
  • Did you use a released version of the library or the version from the develop branch?

    • Version 3.7.3 from develop branch
  • unit tests compile and run successfully
@nlohmann
Copy link
Owner

nlohmann commented May 9, 2020

I can run the example code without problems.

Output:

{
    "13": "test2",
    "7": "test1"
}
  • Branch: current develop branch
  • Compiler: Xcode Version 11.4.1 (11E503a)
  • OS: macOS Catalina 10.15.4 (19E287)

I'll try to check GCC 8.

@nlohmann
Copy link
Owner

nlohmann commented May 10, 2020

I tried some more compilers (all on macOS):

  • [FAIL] g++-6 (Homebrew GCC 6.5.0_5) 6.5.0
  • [FAIL] g++-7 (Homebrew GCC 7.5.0_2) 7.5.0
  • [FAIL] g++-8 (Homebrew GCC 8.4.0_1) 8.4.0
  • [SUCCESS] g++-HEAD (Homebrew GCC HEAD-705510a) 10.0.1 20200405 (experimental)

@stale
Copy link

stale bot commented Jun 9, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 9, 2020
@stale stale bot closed this as completed Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants