Skip to content

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

Closed
@xyz1001

Description

@xyz1001

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions