-
Notifications
You must be signed in to change notification settings - Fork 13.6k
discrepancy between clang and gcc regarding list initialization #72396
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
Comments
@llvm/issue-subscribers-clang-codegen Author: Yaxun (Sam) Liu (yxsamliu)
There is a discrepancy between clang and gcc regarding list initialization when there is only one element in the list:
`#include <stdio.h> class list { list(std::initializer_list<list> elem ) { int main(int argc, char **argv) { list mylist{list{}}; In the above code, clang does not use the list ctor but gcc uses. https://godbolt.org/z/17WaacKG3 https://godbolt.org/z/5nbEP14qv Open this ticket to discuss which behavior is correct. @zygoloid |
According to https://en.cppreference.com/w/cpp/language/list_initialization If T is an aggregate class and the braced-init-list, which does not contain a designated initializer list,(since C++20) has a single element of the same or derived type (possibly cv-qualified), the object is initialized from that element (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization). However, there is some ambiguity about whether this should be applied to the case here. The key point is what "an aggregate class" means here. Is any class "aggregate class" so that this term applies, or "an aggregate class" has to be some specific form of class, e.g. array of class. In the latter case, then this term does not apply. |
msvc behaves the same as clang https://godbolt.org/z/zh3xMcnnb |
It looks like we might be doing copy elision here but I believe we should not be. |
clang -Xclang -ast-dump -fsyntax-only test.cpp | grep "main" -A 10000
Input
|
Agree. It seems "aggregate class" cannot have user-defined constructors (https://en.cppreference.com/w/cpp/language/aggregate_initialization). Therefore in this case the constructor |
Uh oh!
There was an error while loading. Please reload this page.
There is a discrepancy between clang and gcc regarding list initialization when there is only one element in the list:
In the above code, clang does not use the list ctor but gcc uses.
https://godbolt.org/z/17WaacKG3
https://godbolt.org/z/5nbEP14qv
Open this ticket to discuss which behavior is correct.
@zygoloid
The text was updated successfully, but these errors were encountered: