-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
different behaviour between clang and gcc with braced initialization #1059
Comments
There have been previous issues regarding brace initialization, but I can't find them right now. I am not a language lawyer, but maybe someone has an idea on this. |
Results in: What is happening is this constructor:
Is being called when using g++. The initializer_list constructor docs state:
So the issue is coming down to, why is this being selected in the first place?
g++:
clang:
So, we manage to do... something? On clang the most to me that would make sense (I think) would be a single set of square brackets around it, not two (for the j3 case). My intuition is saying g++ is interpreting {(result_of_json_parse)} as an initializer list prior to deducing the type of auto, then it calls the initializer_list constructor with this value to result in a json type rather than a list, allowing for auto deduction. Why this would happen, I'm not entirely sure. I sprinkled some print statements into the constructors to see what is happening like so:
Clang says:
g++-4.9 says:
Where COMPAT_TYPE is referring to:
Whereas g++ 5.4 says:
(Remember at the top, the initializer_list constructor). If we switch the code around to this:
And add explicit qualifier like:
We now get a compile error with g++ and not clang!
So we have something at least. But this compile error doesn't occur unless we do this... so... hmph. On the side, I recommend not using brace initialization with auto... it is somewhat ambiguous and I've encountered multiple confusions from it. I I think this is relevant: http://open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3681.html |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Duplicate of #1359. |
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:
works on clang
works on both family compilers
works on g++
thank you for the support!
The text was updated successfully, but these errors were encountered: