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 declarations, again a waste when you will only have a single object of a given type.
IMO you can still be explicit about field offsets by writing the struct in a usual way, and using static assertions to ensure offsets match the intended layout.
There are two kinds of undefined behaviour being invoked in using this. Its a horrible idea and a horrible code smell, get rid of it if you ever see something like this.
I'm using anonymous nested structs extensively for grouping related items, but I consider the extra field name a feature, not something that should be hidden:
The result is uglier and less maintainable than a pair of macros. Or just stop trying to hide syntax. This is ultimately on the same level as typedefing pointers.
You're right; thanks for noticing and I've updated the first example. My C is a bit rusty these days and I didn't check it with a compiler the way I should have.
Doesn’t matter for C, but in C++ this could make your contexpr functions UB since you can only use one member of a union in constexpr contexts (the “active” member).
Constexpr unions is the sane/safe way to use them. Its great, because accessing a member which isnt the last one written, constexpr will explicitly prevent it compile time. Whereas all other examples here are explicitly undefined behaviour!
This is also known as the most common invocation of undefined behaviour in game programming. If you do this, write to y, then read from [1]. You are invoking undefined behaviour, and compilers doing different things here between windows, linux mac, and different compiler versions is a common cause of "why isnt my game working right on XXX, it works fine on YYY questions.
Bleurgh. 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.
This is probably a terrible idea, remember that if you have written one member of a union, all other members remain public, yet accessing any of them in any way is undefined behaviour. This is made way worse by most compilers mostly choosing to let you do what you think it will. They just dont guarantee they always will or in all cases.
I believe you are mistaken. The C11 standard, section 6.5.2.3 "Structure and union members" pgf 6, says "One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members." And that seems to be what's being used here.
10000truths|4 years ago
WalterBright|4 years ago
gumby|4 years ago
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 declarations, again a waste when you will only have a single object of a given type.
oshiar53-0|4 years ago
nyanpasu64|4 years ago
midjji|4 years ago
flohofwoe|4 years ago
https://github.com/floooh/sokol-samples/blob/bfb30ea00b5948f...
(also note the 'inplace initialization' which follows the state struct definition using C99's designated initialization)
kevin_thibedeau|4 years ago
remram|4 years ago
siebenmann|4 years ago
(I'm the author of the linked-to article.)
sesuximo|4 years ago
ferdek|4 years ago
[0] https://stackoverflow.com/a/25672839
pjmlp|4 years ago
https://shafik.github.io/c++/undefined%20behavior/2019/05/11...
pjmlp|4 years ago
midjji|4 years ago
bruce343434|4 years ago
midjji|4 years ago
PaulHoule|4 years ago
You don't need eval(), you've got strcpy()!
rightbyte|4 years ago
midjji|4 years ago
Subsentient|4 years ago
vbezhenar|4 years ago
I think that C++ is better than C, but C is not that bad, even for large projects.
kktkti9|4 years ago
PostThisTooFast|4 years ago
[deleted]
midjji|4 years ago
drfuchs|4 years ago
adamnemecek|4 years ago
sp332|4 years ago