Điều này giống như lỗi GCC. Tiêu chuẩn nói (9.5p2):
At most one non-static data member of a union may have a brace-or-equal-initializer.
Nếu không, quy tắc sẽ giống như đối với một lớp thông thường.
EDIT: Ngoài ra, 12.6.2p8:
In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then
- if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;
- otherwise, if the entity is a variant member (9.5), no initialization is performed;
- otherwise, the entity is default-initialized (8.5).
Có lẽ các nhà xây dựng mặc định ngầm được xác định đếm đây. Thành viên i
đáp ứng các tiêu chí trong dấu đầu dòng đầu tiên, do đó, nó được khởi tạo như là một thành viên lớp thông thường. Thành viên s
khớp với dấu đầu dòng thứ hai, vì vậy nó không được khởi tạo.
+1 Câu hỏi hay. – Nawaz
[clang] (http://coliru.stacked-crooked.com/view?id=6ab1a0f46b2229dfc46dfa6f4f5e3163-6e9f1f680880347f6708b805c806db62) có vẻ hài lòng với mã của bạn. – Praetorian
@Praetorian, Cảm ơn. Tuy nhiên, bạn chưa đặt câu lệnh đầu ra. Vì vậy, đây là chính xác [đầu ra clang] (http://coliru.stacked-crooked.com/view?id=542928082d0e3f7d6edd525a7fcfac44-6e9f1f680880347f6708b805c806db62). – iammilind