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

error when using nlohmann::json, std::function and std::bind #2147

Closed
3 of 6 tasks
xyz1001 opened this issue May 28, 2020 · 12 comments
Closed
3 of 6 tasks

error when using nlohmann::json, std::function and std::bind #2147

xyz1001 opened this issue May 28, 2020 · 12 comments
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

@xyz1001
Copy link

xyz1001 commented May 28, 2020

What is the issue you have?

g++ 4.8.5 and nlohmann/3.7.3, error: value-initialization of reference type 'const nlohmann::basic_json<>&' when compiler example

#include <iostream>
#include <functional>
#include <map>

#include "nlohmann/json.hpp"

void Foo(const nlohmann::json &j) {
    std::cout<<j.dump()<<std::endl;
}

using Handler = std::function<void(const nlohmann::json&)>;

int main() {
    std::map<int, Handler> handlers;
    handlers.emplace(1, std::bind(Foo, std::placeholders::_1));

    for(auto &i : handlers) {
        i.second(nlohmann::json{{"data", "123"}});
    }
}

Please describe the steps to reproduce the issue.

g++ test.cpp -o main

Can you provide a small but working code example?

here is the simplest example online:

godbolt

What is the expected behavior?

compile pass

And what is the actual behavior instead?

compiler error

Which compiler and operating system are you using?

  • Compiler: g++ 4.8.5, but g++ 8 and 9.1 is ok
  • Operating system: cross compiler for embed linux, host is Manjaro 20.0.1, target os arch is armv7, gcc 4.8.5

Which version of the library did you use?

  • latest release version 3.7.3
  • other release - please state the version: nlohmann/3.7.0
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below
  • I don't try
@nlohmann
Copy link
Owner

Can you please try to compile the test suite? As the used compiler (GCC 4.8.5) is quite old, this may be a bug in its C++11 implementation.

@xyz1001
Copy link
Author

xyz1001 commented May 28, 2020

I tried to cross build unit test and it is ok. @nlohmann

@xyz1001
Copy link
Author

xyz1001 commented May 28, 2020

I used jsonformoderncpp/3.4.0@vthiery/stable before. This version is ok for gcc 4.8.5. However, this vesion is not maintained by conan official. nlohmann_json jsonformoderncpp

@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented May 28, 2020

This is caused by json's template<typename ValueType> operator ValueType() const;, which ends up getting invoked as part of the process of trying to match a json object to the const json& parameter of the bound function.

see #958 for discussion on the topic.

edit I managed to isolate the specific part of the bind process that's failing:

This produces the same error:

void foo() {
    nlohmann::json v;
    std::tuple<nlohmann::json&> x{v};
}

@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented May 28, 2020

In the end, this boils down to the following question:

Should the following be able to compile?

bool test = std::is_default_constructible<std::tuple<int&>>::value;

https://gcc.godbolt.org/z/6vwHuM

I'd say that the answer is evidently yes. And it's pretty obvious to me that it's GCC 4.8.5 that's broken here.

Now the only remaining question is: Is it worthwhile to work around this compiler bug or not? I tried sprinkling a few remove_reference in the code path to no avail. So the answer will most likely be of non-trivial complexity.

My take would be: if we get rid of the allegedly evil operator T(), then this goes away, otherwise, I don't think fixing this is worth the trouble.

@stale
Copy link

stale bot commented Jun 28, 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 28, 2020
@nlohmann
Copy link
Owner

Maybe related: #2226

@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 28, 2020
@stale
Copy link

stale bot commented Jul 29, 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 Jul 29, 2020
@stale stale bot closed this as completed Aug 8, 2020
@wangwenqiangGitHub
Copy link

the json lib version 3.10 still has this isue:
arm-linux-gnueabi/include/c++/4.9.4/tuple:142:42: error: invalid initialization of non-const reference of type 'nlohmann::basic_json<>&' from an rvalue of type 'std::basic_string'
: _M_head_impl(std::forward<_UHead>(__h)) { }

@falbrechtskirchinger
Copy link
Contributor

If someone cares enough to run a git bisect to identify the commit which introduced the problem, I'll take a look. Not making any promises beyond that though.

@wangwenqiangGitHub
Copy link

If someone cares enough to run a git bisect to identify the commit which introduced the problem, I'll take a look. Not making any promises beyond that though.
I use this

#include <iostream>
#include <functional>
#include <map>

#include "nlohmann/json.hpp"

void Foo(const nlohmann::json &j) {
    std::cout<<j.dump()<<std::endl;
}

using Handler = std::function<void(const nlohmann::json&)>;

int main() {
    std::map<int, Handler> handlers;
    handlers.emplace(1, std::bind(Foo, std::placeholders::_1));

    for(auto &i : handlers) {
        i.second(nlohmann::json{{"data", "123"}});
    }
}

error:
arm-linux-gnueabi/include/c++/4.9.4/tuple:132:22: error: value-initialization of reference type 'const nlohmann::basic_json<>&'
: _M_head_impl() { }
^

@falbrechtskirchinger
Copy link
Contributor

Can you run https://git-scm.com/docs/git-bisect on this code?

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

5 participants