title: emulating thiscall in C to achieve struct functions without self-referencing
body: <p>This is relating to the flurry of object-oriented C postings, but differs in that I don't want all the features, just one:</p>
  
  <p>An ability to do this:</p>
  
  <pre><code>struct foo { 
    int (*bar)(void);
    char baz;
  };
  </code></pre>
  
  <p>An then have the back-reference.  C has a calling convention called cdecl; basically pushes arguments on to the stack, has a return pointer, then jumps to some address.  The code at that address pops arguments off the stack and goes on its merry way.</p>
  
  <p>The thiscall convention is slightly different in that it adds one extra argument, a "this" pointer, implicitly.</p>
  
  <p>Since you can start executing arbitrary byte code in C quite easily AND since gcc supports inline assembler templating, this sounds like you could just make some macro so that you could do something like:</p>
  
  <pre><code>int bar(void) {
    GETTHIS;
    cThis-&gt;baz = 0;
  }
  
  int createFoo(struct Foo*pFoo) {
    ASSIGN(pFoo, bar);
  } 
  </code></pre>
  
  <p>Basically what ASSIGN would do is sidestep cdecl in someway to emulate a thiscall style convention and then what GETTHIS would do is the other side of the accounting trick.</p>
  
  <p>I was wondering if:</p>
  
  <ul>
  <li>A solution to this exists and</li>
  <li>If not, if there's a reason why it can't be done</li>
  </ul>
  
  <p>Just this alone; the convenience of true "We're all consenting adults here" style member functions, would be simply awesome. Thanks!</p>
  
  <p>Notes:</p>
  
  <ul>
  <li>I'm just talking x86, linux, gcc here ... I know the world is a wide and strange place.</li>
  <li>This is <strong>out of pure curiosity</strong>.</li>
  </ul>