How to create type safe enums? : <p>To achieve type safety with enums in C is problematic, since they are essentially just integers. And enumeration constants are in fact defined to be of type <code>int</code> by the standard.</p>

<p>To achieve a bit of type safety I do tricks with pointers like this:</p>

<pre><code>typedef enum
{
  BLUE,
  RED
} color_t;

void color_assign (color_t* var, color_t val) 
{ 
  *var = val; 
}
</code></pre>

<p>Because pointers have stricter type rules than values, so this prevents code such as this:</p>

<pre><code>int x; 
color_assign(&amp;x, BLUE); // compiler error
</code></pre>

<p>But it doesn't prevent code like this:</p>

<pre><code>color_t color;
color_assign(&amp;color, 123); // garbage value
</code></pre>

<p>This is because the enumeration constant is essentially just an <code>int</code> and can get implicitly assigned to an enumeration variable. </p>

<p>Is there a way to write such a function or macro <code>color_assign</code>, that can achieve complete type safety even for enumeration constants?</p>
