News for hackers. Stuff that matters.
Re:Tagged unions, or ‘variants’, in C by wyufro on Monday June 26, 2017 @07:22AM (#14635866)
> It relies on struct layout, and if anything has wider alignment than ptrdiff_t, it's broken.

I think you're wrong about this. I'm pretty certain a pointer to a struct is always equivilent to a pointer to the first member, except for the type. I went and looked it up in C11 (6.7.2.1 point 15):

> A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

Edit: A duh, the + sizeof(ptrdiff_t) is the part that's wrong. Yeah, agreed.

Re:Tagged unions, or ‘variants’, in C by rkangel on Monday June 26, 2017 @08:41AM (#14636266)
Yes - this is what allows you to some semblance of struct inheritance. You can embed a structure as the first member in the 'derived' structure and then up and down cast as appropriate.