Hi, Yair Lenga via Gcc <gcc@gcc.gnu.org> writes:
> I wonder if it will be possible to add support for "scoped" enum to GCC. > The current C standard has one name space for all enums, and different name > space for the members of each "struct". As a result, possible to say > > struct foo { int a } ; > struct bar { double a }; // This is different 'a', different type > > But illegal to to (ignoring the conversion to use all upper for enum). > > enum a { u, v } ; > enum b { v, w } ; // can not do this, as 'v' must be distinct > > One annoying side effect is that any package/module creating an enum has to > worry about namespace collision with everyone else in the world. Common > practices include distinct prefixes, similar to the way different libraries > use distinct prefixes to avoid function name collision. This solution is > far from perfect and leads to excessive long enum name. > > A reasonable wish list - add a magic keyword that will place the enums into > a scope, so that the following work: > > SCOPED enum shirt_sz { small, medium, large } ; > SCOPED enum shoe_sz { small, medium, medium_wide, large, xlarge } ; > > enum shirt_sz tshift_size = shift_sz.medium ; > enum shoe_siz boot_size = shoe_sz.xlarge ; > > Not perfect, but not complex, will make enum reusable across many scenario, > where they are currently hard to implement - because of namespace conflict > - between system header and user headers, between different packages. > > A smart compiler can also alert when "types" are mixed (assign value from > shift_sz to a variable of type shoe_sz). Not critical - as my understanding > is that this is not enforced today. For the case that an enum symbol is > distinct (in the current compilation unit), the compiler can allow using it > without the namespace - practically falling back into current behavior. > > Feedback ? Anyone know how to get a prototype into gcc ? How one get > approval for such "extensions". I'd suggest, if you choose to implement this, to imitate what C++ does for these, maybe even propose it for the standard. There's already established syntax and semantics. It would certainly be nice to have such a thing in C. Have a lovely evening. -- Arsen Arsenović
signature.asc
Description: PGP signature