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

Copy-construct using initializer-list converts objects to arrays #1359

Closed
arrtchiu opened this issue Nov 15, 2018 · 6 comments
Closed

Copy-construct using initializer-list converts objects to arrays #1359

arrtchiu opened this issue Nov 15, 2018 · 6 comments

Comments

@arrtchiu
Copy link

I seem to have hit a bug in either this library / MSVC. Here's a minimal repro case:

struct MyObj {
 json mJson;
 explicit MyObj(json const& input) : mJson{input} {}
};

json j = json::object();
j["foo"] = "bar";
j["baz"] = "ba";
auto x = std::make_shared<MyObj>(j);

std::cout << obj->mJson.dump() << std::endl;

Output:

J: {"baz":"bah","foo":"bar"}
J: [{"baz":"bah","foo":"bar"}]

Interestingly, changing the initialiser from mJson{input} to mJson(input) produces the correct output:

J: {"baz":"bah","foo":"bar"}
J: {"baz":"bah","foo":"bar"}

I'm currently using 3.1.2 but confirmed it also reproduces on latest 3.4.0

@theodelrieu
Copy link
Contributor

theodelrieu commented Nov 15, 2018

Indeed, using braces when an initializer_list constructor is present will pick that constructor, this is a bit tricky.

@nlohmann
Copy link
Owner

This is expected behavior, I'm afraid.

@arrtchiu
Copy link
Author

Crikey thanks for the amazingly fast reply! I guess the solution is not to use braces for initialising members!

@arrtchiu
Copy link
Author

Looks like a Clang bug, since the output on Clang for both variants of the code is:

J: {"baz":"bah","foo":"bar"}
J: {"baz":"bah","foo":"bar"}

GCC behaves the same as MSVC in this case.

I'll file a bug over there. Thanks again for the fast replies!

@nlohmann
Copy link
Owner

Great! Please let us know about the progress of the filed bugs!

@arrtchiu
Copy link
Author

Clang developers pointed me to this: https://clang.llvm.org/cxx_dr_status.html#2137

Sounds like it’s an unresolved ambiguity in the c++ spec itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants