4 ms·
Can somebody explain the significance of this to me?
by grifaton 15y ago
Can somebody explain the significance of this to me?
- ramchip 15y agoIf I understand correctly, GCC now has an extra switch to support some additions to regular C that the Plan 9 compiler offers related to the manipulation of anonymous fields in a structure. I don't think it has much significance for the vast majority of C programmers out there. I'm not sure why this is first place on the front page, to be honest...
- pmjordan 15y agoIt would have a positive effect on the readability and conciseness of our (SSDCache) code if we could actually use compilers supporting this. Unfortunately, we're stuck with older versions of GCC for the foreseeable future, and probably MSVC when we port to Windows. In hindsight I'm wishing we'd spent some time up-front getting to grips with something like SC [1] for generating C code from something slightly higher level to avoid some of the language's shortcomings that make the code overly convoluted. (some kind of usable closure-like mechanism would be fantastic, for example - callback-based asynchronous I/O isn't particularly fun in C) [1] http://super.para.media.kyoto-u.ac.jp/~tasuku/sc/index-e.html http://super.para.media.kyoto-u.ac.jp/~tasuku/sc/index-e.htm...
- rm445 15y agoThere's a lot of residual good will for Plan 9, because of the people involved, and because of the way many of its good ideas have work their way into other OSes (despite Plan 9 itself's relative lack of success). So ideas from Plan 9 will always have an audience. As for top of the front page, it's just how things bubble up and down, no cause for alarm.
- pmjordan 15y agoThe stuff described on the first part of that page has been supported by GCC for ages. It's useful where structs and unions are deeply nested, e.g.: enum MyStructType { MyInt, MyFloat }; struct my_struct { enum MyStructType type; union { struct { int my_int; } as_int; struct { float my_float; } as_float; } d; }; To get to the actual data, in standard C89/99 you need to do this: struct my_struct* s = /* ... */; if (s->type == MyInt) printf("%i\n", s->d.as_int.my_int); /* <- !!!!!! */ else printf("%f\n", s->d.as_float.my_float); /* <- !!!!!! */ With the extension, the inner union and struct members are added to the outer struct/union's namespace: enum MyStructType { MyInt, MyFloat }; struct my_struct { enum MyStructType type; union { struct { int my_int; }; struct { float my_float; }; }; /* note: no name */ }; Now the access code is much nicer: struct my_struct* s = /* ... */; if (s->type == MyInt) printf("%i\n", s->my_int); /* <- !!!!!! */ else printf("%f\n", s->my_float); /* <- !!!!!! */ The second part extends this to typedefs, while the third part, the Plan9 addition (which is presumably the reason for the HN submission) gives you an implicit struct pointer conversion for structs composed of other structs, a bit like C++'s implicit pointer conversion for inheritance. The page has an example of this already: struct s1 { int a; }; struct s2 { struct s1; }; extern void f1 (struct s1 *); void f2 (struct s2 *p) { f1 (p); } This is how you'd normally have to do it: struct s1 { int a; }; struct s2 { struct s1 my_s1; }; // <- note non-anonymous struct field extern void f1 (struct s1 *); void f2 (struct s2 *p) { f1 (&p->my_s1); } // <- my code is always littered with this idiom All of this might not seem like much, but complicated data structures really aren't a forte of the C syntax. A bit of sugar alleviates at least some of the endless repetition.