Skip to content

different behaviour between clang and gcc with braced initialization #1059

Description

@scontini76
#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

int main()
{
    auto j{json::parse(R"({ "result": [1, 2, 3] })")};

    std::cout << j["result"];

    return 0;
}

compiled with g++5, g++6, gcc+7 bring to the following result:

terminate called after throwing an instance of 'nlohmann::detail::type_error'
what(): [json.exception.type_error.305] cannot use operator[] with array

with clang++ the output is the excepted:

[1, 2, 3]

I'm using json 3.1.2, is there an error in my code? what compiler is wrong? can we obtain the same behaviour on both the family complier?

The code works in the on either g++ and clang++ if I use the assignment instead of braced initialisation.

so:

auto j{json::parse(R"({ "result": [1, 2, 3] })")};
std::cout << j["result"];

works on clang

auto j = json::parse(R"({ "result": [1, 2, 3] })");
std::cout << j["result"];

works on both family compilers

auto j{json::parse(R"({ "result": [1, 2, 3] })")};
std::cout << j[0]["result"];

works on g++

thank you for the support!

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: duplicatethe issue is a duplicate; refer to the linked issue instead

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions