Anonymous nested structs are also quite useful for creating struct fields with explicit offsets: #include #include #define YDUMMY(suffix, size) char dummy##suffix[size] #define XDUMMY(suffix, size) YDUMMY(suffix, size) #define PAD(size) XDUMMY(__COUNTER__, size) struct ExplicitLayoutStruct { union { struct __attribute__((packed)) { PAD(3); uint32_t foo; }; struct __attribute__((packed)) { PAD(5); uint16_t bar; }; str…
Do foo and bar deliberately overlap?
Learning that you can use unions in C for grouping things into namespaces
21–30 of 150 posts
Re: Learning that you can use unions in C for grouping things into namespaces
#22Bleurgh. I have a deep soft spot for C, and I'm known to get twisted pleasure from using obscure language features in new ways to annoy people, but this is a level of abuse that even I can't get behind. If you need namespacing, use C++. As much as I love C, it's terrible for large projects.
Re: Learning that you can use unions in C for grouping things into namespaces
#23Re: Learning that you can use unions in C for grouping things into namespaces
#24Anonymous nested structs are also quite useful for creating struct fields with explicit offsets: #include #include #define YDUMMY(suffix, size) char dummy##suffix[size] #define XDUMMY(suffix, size) YDUMMY(suffix, size) #define PAD(size) XDUMMY(__COUNTER__, size) struct ExplicitLayoutStruct { union { struct __attribute__((packed)) { PAD(3); uint32_t foo; }; struct __attribute__((packed)) { PAD(5); uint16_t bar; }; str…
One of the very few things from C that I miss in C++ is anonymous structs and enums. I really don’t understand why they are not allowed. That is, C style enums don’t have to have a name but “type safe” (enum class) ones do. One classic use is to name an otherwise boolean option in a function signature; there’s typically no need to otherwise name it. C++ incompatibly requires a name for all struct and class declaratio…
You're right about "enum class", but anonymous classes and structs are perfectly valid in C++:
Re: Learning that you can use unions in C for grouping things into namespaces
#25Bleurgh. I have a deep soft spot for C, and I'm known to get twisted pleasure from using obscure language features in new ways to annoy people, but this is a level of abuse that even I can't get behind. If you need namespacing, use C++. As much as I love C, it's terrible for large projects.
I think that C++ is better than C, but C is not that bad, even for large projects.
Re: Learning that you can use unions in C for grouping things into namespaces
#26Re: Learning that you can use unions in C for grouping things into namespaces
#27Bleurgh. I have a deep soft spot for C, and I'm known to get twisted pleasure from using obscure language features in new ways to annoy people, but this is a level of abuse that even I can't get behind. If you need namespacing, use C++. As much as I love C, it's terrible for large projects.
Linux kernel is large project and clearly C is sufficient for it, given the fact that migrating to C++ would probably be very easy (not using all C++ features, but just selected ones), yet it did not happen. I think that C++ is better than C, but C is not that bad, even for large projects.
Re: Learning that you can use unions in C for grouping things into namespaces
#28Earlier quoted context omitted.
One of the very few things from C that I miss in C++ is anonymous structs and enums. I really don’t understand why they are not allowed. That is, C style enums don’t have to have a name but “type safe” (enum class) ones do. One classic use is to name an otherwise boolean option in a function signature; there’s typically no need to otherwise name it. C++ incompatibly requires a name for all struct and class declaratio…
> C++ incompatibly requires a name for all struct and class declarations You're right about "enum class", but anonymous classes and structs are perfectly valid in C++: https://godbolt.org/z/7MbcqhnoK
struct S { struct { int x; }; };
under -pedantic and you'll get warning: ISO C++ prohibits anonymous structs [-Wpedantic]Re: Learning that you can use unions in C for grouping things into namespaces
#29Earlier quoted context omitted.
In C++ we have namespaces for 30 years now, no need for such tricks.
Hmm. How do C++ namespaces help with the structure naming problem in this example? They seem completely orthogonal. C++ namespaces are a way to avoid library A's symbol "cow" clashing with library B's symbol "cow" without everything being named library_a_cow and library_b_cow all over the place which is annoying. I agree C would be nicer with such a namespace feature. However this technique is about what happens when…