Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is incredibly common in embedded programming. Lots of times you'll have some binary packet come over the wire, the first field is a packet type specifier, you can then just cast the rest of your byte buffer to the proper type.

It is very unfortunate that C (and until recently C++) did not have a way to specify the size of enums. Basically this has resulted in crappy #defines being used for far too many years.

I always called what you did above "tagged types". A number of type systems actually work like that under the covers, objects get a type ID, and then a lookup function figures out what that type ID maps to.



The more common term is "tagged union". http://en.wikipedia.org/wiki/Tagged_union


> It is very unfortunate that C (and until recently C++) did not have a way to specify the size of enums.

It had compiler specific switches to set the size of all enums globally, which helped exactly nobody.


I agree, this typed enum in C++11 made a MachO manipulation library much nicer.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: