
Plan9 compiler extensions in gcc 4.6 - toni
http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Unnamed-Fields.html#Unnamed-Fields
======
pmjordan
The last part is pretty cool (the first bit has been supported by GCC for
years), though it would be even more helpful to be able to do the reverse
concisely - convert a pointer to the contained struct to a pointer to the
containing one. In my C code I invariably use (something similar to) the
container_of() macro used in the Linux kernel [1]. The primary use is in
callbacks associated with a particular object that can be embedded in larger
structures.

Right now, say a library I'm using exposes this:

    
    
      struct lib_struct {
        int foo;
        char* bar;
      };
      typedef void (*lib_callback)(struct lib_struct*);
      extern void lib_fn(struct lib_struct* obj, lib_callback cb);
    

If I want to use that in one of my own data structures:

    
    
      struct my_struct {
        struct lib_struct lib_st;
        int my_own_data;
      };
    
      static void my_callback(struct lib_struct* lib_st)
      {
        // plan9 extension does not appear to help here...
        struct my_struct* me = container_of(lib_st, struct my_struct, lib_st);
        printf("%i\n", me->my_own_data);
      }
    
        /* elsewhere... */
        struct my_struct* m = /* ... */;
        // ... though it would here:
        lib_fn(&m->lib_st, my_callback);
    

It'd be handy to be able to condense the container_of line somewhat, while
maintaining (some) type safety.

[1] <http://www.kroah.com/log/linux/container_of.html> \- I use a similar
macro that preserves NULL pointers.

------
grifaton
Can somebody explain the significance of this to me?

~~~
ramchip
If 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
It 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.htm...](http://super.para.media.kyoto-u.ac.jp/~tasuku/sc/index-e.html)

------
coliveira
These features may be used to program in a more OO style without the notion of
classes. For example, one case use the embedding of fields to have a similar
effect to inheritance.

Similarly, one can pass pointers to objects that contain a type as if it was
the type itself, which is the same thing that inheritance allows. Of course,
here there is no difference between containment and inheritance, you can use
it as you desire.

