Earlier quoted context omitted.
Type punning is not undefined, it's implementation defined in C. In practice, every major C compiler will be fine with type punning, though it may disable some optimizations. The story is different in C++, but in practice many compilers support it the same as in C. Especially for games, where VC++ (PC, Xbox) and Clang (PS4/PS5) are the most commonly used compilers, it also works as expected. The trick is to only use…
Something being very common and a very common source of portability issues isn't exactly contradictory. Its a bad idea, and it is outright being taught in modern game programming courses that its a bad idea, but common in older guides, specifically because it caused so many problems. Im pissed at this specific construct because I got it handed to me in a huge game library and had to spent a long time figuring out why…
Learning that you can use unions in C for grouping things into namespaces
141–150 of 150 posts
Re: Learning that you can use unions in C for grouping things into namespaces
#142Earlier quoted context omitted.
Use a enum in a namespace, or anonymous namespace
This is an example of the desired use case: static obj& some_call (obj& o, enum struct { abandon, save } disposition) { ... }; This is a common case (and should be more common) to avoid using an obscure boolean flag, which can lead to bugs. It shouldn't need a name. An anonymous namespace just means the name itself won't leak out; under C++ rules I need the name even to specify the enum tag, which is absurd.
Re: Learning that you can use unions in C for grouping things into namespaces
#143Earlier quoted context omitted.
From the standard, the enum-name is marked as optional in the enum grammar: in [dcl.enum]( https://eel.is/c++draft/dcl.enum#11 ) ; it is also referenced e.g. in [dcl.dcl]: > An unnamed enumeration that does not have a typedef name for linkage purposes ([dcl.typedef]) and that has a first enumerator is denoted, for linkage purposes ([basic.link]), by its underlying type and its first enumerator; such an enumeration is…
This is awesome. I referenced cppreference, but that is not authoritative. Unfortunately, in the final draft, [class.pre] grammar makes the name mandatory even though the language you quote remains in the first textual paragraph following the grammar specification! The part of enums you quoted was C-compatible enums; anonymous scoped enums are explicitly forbidden: "The optional enum-head-name shall not be omitted in…
That's pretty strange, considering e.g. this paper for quite some time ago: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p022... which is written as if anonymous structs always were a thing.
I wonder if there isn't a deep confusion somewhere where "anonymous" and "unnamed" mean different things to different persons.
Re: Learning that you can use unions in C for grouping things into namespaces
#144Earlier quoted context omitted.
Op probably means cpp, where it is indeed undefined behavior. not sure about c. I doubt that if this would cause a "my game does not work on XXX" though. Is there really a compiler out there that will handle such abuse differently?
yes its undefined behaviour in both C and C++. Yes, a number of compilers treat this differently, its also poorly supported on custom hardware using standard compilers like gcc. So compiling for some mobile device with slightly custom ... good luck.
Re: Learning that you can use unions in C for grouping things into namespaces
#145Earlier quoted context omitted.
I'm referring to both syntax based (AST) macros, and text based (preprocessor) macros. The latter, of course, are much worse. An example of the former is so-called "expression templates" in C++. I've seen them used to create a regular expression language using C++ expression templates. The author was quite proud of them, and indeed they were very clever. However nice the execution, the concept was terrible. There was…
Can you link to it? We're using expression templates on a new library and I find it useful.
The speaker was proud and beaming for showing how powerful C++ is and the audience was in awe.
I was incredulous! Jaw open! The "solution" was horrible with a bunch of workarounds for a bunch of shortcomings. It was a "the emperor does not have cloths" moment for me.
Boost Spirit may be better today with newer C++ features; I don't know.
Re: Learning that you can use unions in C for grouping things into namespaces
#146Earlier quoted context omitted.
We're making a numerical computing library that expression templates are used for compile-time evaluation of expressions. It's a very niche hpc library, so it might be one of the few places it's appropriate.
I wish you luck with it. If you send me an email, when I find what I wrote about it I'll pass it along to you.
Re: Learning that you can use unions in C for grouping things into namespaces
#147Earlier quoted context omitted.
Can you link to it? We're using expression templates on a new library and I find it useful.
I don't know what code Walter is referring to but I had similar experiences during a presentation of the Boost Spirit library (at our Silicon Valley C++ Meeting; must be about a decade ago now). The speaker was proud and beaming for showing how powerful C++ is and the audience was in awe. I was incredulous! Jaw open! The "solution" was horrible with a bunch of workarounds for a bunch of shortcomings. It was a "the em…
Re: Learning that you can use unions in C for grouping things into namespaces
#148Earlier quoted context omitted.
This is an example of the desired use case: static obj& some_call (obj& o, enum struct { abandon, save } disposition) { ... }; This is a common case (and should be more common) to avoid using an obscure boolean flag, which can lead to bugs. It shouldn't need a name. An anonymous namespace just means the name itself won't leak out; under C++ rules I need the name even to specify the enum tag, which is absurd.
Each instance of "enum struct { abandon, save }" would denote a different type, yes? How would you write a compatible definition to go with your prototype?
The point is to prevent the “mysterious bool arguments” class of error.
The question is if ADL could infer the scope of the enum, as template instant is toon can now infer the right thing and don’t always need the notation.
Re: Learning that you can use unions in C for grouping things into namespaces
#149Earlier quoted context omitted.
The only explanation I saw was that C++ standards guys were horrified by the idea of unpredictable side effects as a result of initialization of a struct. I think C++ though is adding them. What I'd like in c is designated function parameters. // these the same bar(.a = 10, .b = 12); bar(.b = 12, .a = 10);
> The only explanation I saw was that C++ standards guys were horrified by the idea of unpredictable side effects as a result of initialization of a struct. I don't understand. How would struct or class initialization be any different from simply doing, say, `for (auto& a : { x, y, z }) frob (a);` which is perfectly legal?
Re: Learning that you can use unions in C for grouping things into namespaces
#150Earlier quoted context omitted.
Each instance of "enum struct { abandon, save }" would denote a different type, yes? How would you write a compatible definition to go with your prototype?
I don’t care that they are different types; if anything that would be a feature. The point is to prevent the “mysterious bool arguments” class of error. The question is if ADL could infer the scope of the enum, as template instant is toon can now infer the right thing and don’t always need the notation.
/* example.h */
void f(enum struct { x, y } arg);
/* example.c */
void f(enum struct { x, y } arg) {
/* do something with arg */
}
…then you've just created a function with two different overloadings based on distinct anonymous types which just happen to be spelled the same way. Without a type name I don't see any way you could define a function whose prototype would be compatible with the forward declaration. You also have conflicting definitions of "x" and "y" with the same names and scope but different types. Perhaps with GNU extensions you could use typeof(x) for the argument and avoid the conflict, but that isn't standard C++.