Is it possible to implement GNU C's typeof(x) with C11's _Generic?
<p>To make some code compile in C and C++ I use this in a few places:</p>

<pre><code>#ifdef __cplusplus 
    #define typeof(x) decltype(x) // works ok in most cases, except C++ reference types
#endif 

char* a = (typeof(a)) malloc(4);
</code></pre>

<p>In C, this compiles to <code>char* a = (char *) malloc(4)</code> where the cast is totally unecessary, but in C++ <code>void *</code> is not implicitly promoted to <code>char *</code> and an error is issued if a cast is not present. </p>

<p>This is just as well when I can compile with <code>-std=gnu11</code> on GCC or Clang, but what when I want to make my code compile as ISO C11? I thought I could use C11's <code>_Generic</code> to implement <code>typeof(x)</code> to cast some types:</p>

<pre><code>#define gettype(x) _Generic((x), \
  short:       (short ), \
  char:        (char  ), \
  char*:       (char *), \
  default:     (void *)  )

int main (void) {
  short a = (gettype(a)) 1;

  return a;
}
</code></pre>

<p>But no matter what type defined in <code>gettype(x)</code> is given in <code>a</code>'s declaration, </p>

<pre><code>typeof.h: In function ‘main’:
typeof.h:2:24: error: expected expression before ‘,’ token
   short:       (short ), \
                        ^
typeof.h:8:13: note: in expansion of macro ‘gettype’
   char a = (gettype(a)) 1;
             ^~~~~~~
typeof.h:8:25: error: expected ‘,’ or ‘;’ before numeric constant
   char a = (gettype(a)) 1;
</code></pre>

<p><code>gcc -E</code> says that line expands just fine: </p>

<pre><code>short a = (_Generic((a), short: (short ), char: (char ), char*: (char *), default: (void *) )) 1;                             ^
</code></pre>

<p>Is there some syntax I am missing, or is it simply not possible in C to generate cast code using <code>_Generic</code>?</p>