Skip to content

json({}) gives null instead of empty object with GCC and -std=c++17 #2046

@misos1

Description

@misos1
  • What is the issue you have?

Readme says:

// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();

But seems when using GCC with -std=c++17 json({}) actually gives null. Without -std=c++17 or with -std=c++14 or any other version it works. On clang it works with any version.

Seems there is some problem with overload resolution because basic_json(std::nullptr_t = nullptr) is called instead of basic_json(initializer_list_t init, bool type_deduction = true.... It works after I explicitly set second parameter to true.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
#include "json.hpp"
int main()
{
	nlohmann::json j = nlohmann::json({});
	printf("%s: %s\n", j.type_name(), j.dump().c_str());
	return 0;
}
g++ -std=c++17 main.cpp
  • What is the expected behavior?
object: {}
  • And what is the actual behavior instead?
null: null

g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

  • Did you use a released version of the library or the version from the develop branch?

version 3.7.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmationstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updatedstate: waiting for PR

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions