News for hackers. Stuff that matters.
Re:Switching from C++ to Rust by tialaramex on Tuesday March 14, 2023 @05:27AM (#35149042)
I'm not certain for C, but definitely in C++ it's legal to union a bunch of structures with a common prefix, and then talk about the prefix in the "wrong" variant and that's OK. There may be some restrictions about exactly what is in that prefix, but at least obvious things like an enum or an integral type will work.

So for your example you put ShapeType type in each of Rectangle, Circle, Triangle etc. and then you can union all of them, and the language promises that shape.circle.type == Rectangle is a reasonable thing to ask, so you can use that to make a discriminated union.

Re:Switching from C++ to Rust by staunton on Tuesday March 14, 2023 @06:50AM (#35149571)
> I'm not certain for C, but definitely in C++ it's legal to union a bunch of structures with a common prefix, and then talk about the prefix in the "wrong" variant and that's OK

That doesn't sound right to me. Do you have a source? Is that in the standard?

Re:Switching from C++ to Rust by unnah on Tuesday March 14, 2023 @07:57AM (#35150093)
I don't know about the standard, but if cppreference.com is good enough: At https://en.cppreference.com/w/cpp/language/union it says "If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler."
Re:Switching from C++ to Rust by tialaramex on Tuesday March 14, 2023 @09:22AM (#35151032)
I of course do not own a copy of the expensive ISO document, however, in the draft:

11.5.1 [class.union.general]

[Note 1: One special guarantee is made in order to simplify the use of unions: If a standard-layout union contains several standard-layout structs that share a common initial sequence ([class.mem]), and if a non-static data member of an object of this standard-layout union type is active and is one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of the standard-layout struct members; see [class.mem]. — end note]

Re:Switching from C++ to Rust by unwind on Tuesday March 14, 2023 @08:00AM (#35150119)
Yes, totally. Good point, I forgot about that technique. I prefer this, which is less invasive.