title: Macro expansion of __typeof__ to function name
body: <p>I wrote the following code in plain <strong>C</strong>:</p>
  
  <pre><code>#define _cat(A, B) A ## _ ## B
  #define cat(A, B) _cat(A, B)
  #define plus(A, B) cat(cat(plus,__typeof__(A)),__typeof__(B))(A, B)
  
  int main(int argc, const char * argv[])
  {
      double x = 1, y = 0.5;
      double r = plus(x, y);
      printf("%lf",r);
      return 0;
  }
  </code></pre>
  
  <p>Here, I would like the macro <code>plus</code> to be expanded becoming a function name which contains the types of the parameters. In this example I would like it to expand the following way</p>
  
  <pre><code>double r = plus(x, y)
  ...
  /* First becomes*/
  double r = cat(cat(plus,double),double)(x, y)
  ...
  /* Then */
  double r = cat(plus_double,double)(x, y)
  ...
  /* And finally */
  double r = plus_double_double(x, y)
  </code></pre>
  
  <p>However all I got from the preprocessor is</p>
  
  <pre><code>double r = plus___typeof__(x)___typeof(y)(x,y)
  </code></pre>
  
  <p>and gcc will obviously refuse to compile.
  Now, I know that <strong>typeof</strong> evaluates at compile-time and it is my understanding that a macro is only prevented from being evaluated when it is contained in second macro which directly involves the stringify <code>#</code>and the concatenation <code>##</code> tokens (here's the reason why I split <code>cat</code> in the way you see). If this is right, why doesn't <code>__typeof__(x)</code> get evaluated to double by the preprocessor? Seems to me that the behaviour should be perfectly clear at build time. Shouldn't <code>__typeof__(x)</code> evaluate to <code>double</code> before even going in <code>_cat</code>?</p>
  
  <p>I searched and searched but I couldn't find anything... Am I doing something really really stupid?</p>
  
  <p>I'm running Mac OS X Mountain Lion but I'm mostly interested in getting it work on any POSIX platform.</p>