anonymous-struct

C++ anonymous structs

巧了我就是萌 提交于 2019-12-22 04:03:06
问题 I use the following union to simplify byte, nibble and bit operations: union Byte { struct { unsigned int bit_0: 1; unsigned int bit_1: 1; unsigned int bit_2: 1; unsigned int bit_3: 1; unsigned int bit_4: 1; unsigned int bit_5: 1; unsigned int bit_6: 1; unsigned int bit_7: 1; }; struct { unsigned int nibble_0: 4; unsigned int nibble_1: 4; }; unsigned char byte; }; It works nice, but it also generates this warning: warning: ISO C++ prohibits anonymous structs [-pedantic] Ok, nice to know. But.

C++ private modifier ignored on nested anonymous struct

孤人 提交于 2019-12-05 14:17:42
问题 The following sample code compiles just fine in Visual C++: class Test { private: struct { struct { int privateData; }; }; }; int main(int, char **) { Test test; test.privateData = 0; return 0; } But why? I'd expect a compiler error because the privateData member should be inaccessible to the function main, since it's supposed to be private like its container's container. I know that nameless structs are not part of official C++, but this design is asinine. By the way I've also tried to

C++ anonymous structs

感情迁移 提交于 2019-12-05 03:37:23
I use the following union to simplify byte, nibble and bit operations: union Byte { struct { unsigned int bit_0: 1; unsigned int bit_1: 1; unsigned int bit_2: 1; unsigned int bit_3: 1; unsigned int bit_4: 1; unsigned int bit_5: 1; unsigned int bit_6: 1; unsigned int bit_7: 1; }; struct { unsigned int nibble_0: 4; unsigned int nibble_1: 4; }; unsigned char byte; }; It works nice, but it also generates this warning: warning: ISO C++ prohibits anonymous structs [-pedantic] Ok, nice to know. But... how to get this warning out of my g++ output? Is there a possibility to write something like this

Is this a C11 anonymous struct?

老子叫甜甜 提交于 2019-12-01 17:24:42
I was looking into the C11 draft and it says An unnamed member of structure type with no tag is called an anonymous structure; an unnamed member of union type with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. So I constructed the following testcase // struct type with no tag typedef struct { unsigned char a; unsigned char b; // ... Some other members ... unsigned char w; } AToW; union { AToW; // <- unnamed member unsigned char bytes[sizeof(AToW)]; } myUnion; Clang and GCC both complain

Is this a C11 anonymous struct?

馋奶兔 提交于 2019-12-01 17:08:48
问题 I was looking into the C11 draft and it says An unnamed member of structure type with no tag is called an anonymous structure; an unnamed member of union type with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union. So I constructed the following testcase // struct type with no tag typedef struct { unsigned char a; unsigned char b; // ... Some other members ... unsigned char w; } AToW; union {