Skip to content

Usage with -fno-elide-constructors causes dump() output to be array of nulls #2387

Closed
@Qix-

Description

@Qix-

What is the issue you have?

The following program...

#include "./json.hpp"

#include <iostream>

nlohmann::json makeobj() {
        return {
                {"type", "Foo"},
                {"location", {10, 20}}
        };
}

int main() {
        std::cout << makeobj().dump() << std::endl;
        return 0;
}

... compiled with the following flags ...

$ c++ -Wall -Wextra -Werror -std=c++11 -Wshadow -pedantic-errors -fno-elide-constructors -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations -fvisibility=hidde
n -Wunused -g3 -O0 -otest-json test-json.cc

... produces the following output:

[[null,null],[null,[null,null]]]

Omitting the -fno-elide-constructors flag, we get the following (correct) output:

{"location":[10,20],"type":"Foo"}

I've since removed the -fno-elide-constructors from the build script as it was leftover from another project and isn't necessary (or really a good idea) for this project. I just figured you might want to know about it.

I don't fully understand the nuances of the flag but I would imagine it's not a flag that'd be "compatible" with this library (understandably so). At the very least, some documentation in the Notes section in the readme might be a worthwhile PR as it was the first place I looked for an explanation of the null output. Caused a 15 minute headache tracking it down - not terrible, but just annoying enough to open this issue :)

Thanks for the great library!

Which compiler and operating system are you using?

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.1 LTS"

$ c++ --version
Ubuntu clang version 12.0.0-++20200829052612+bf21ce7b908-1~exp1~20200829153238.148
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin

Which version of the library did you use?

Commit 1bcabd9; single include header vendored into project.

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions