title: Function pointer as "tag" in tagged union
body: <p>One of the reasons why I tend to avoid using tagged unions is that I don't like the idea of the performance penalty that the <code>switch/case</code> statement for the tag might introduce if the number of tags is greater than 4 or so.</p>
  <p>I just had the idea that instead of using a tag, one could set a pointer to a function that reads the last  written value in the <code>union</code>. For example:</p>
  <pre><code>union u{
     char a;
     int b;
     size_t c;
  };
  
  struct fntag{
     void (*readval)(union u *ptr, void *out);
     union u val;
  };
  </code></pre>
  <p>And then, whenever you write a value into <code>val</code>, you also update the <code>readval</code> pointer accordingly, so that it points to a function that reads the last field you wrote in the union. Yes, there's a difficult issue, and it's where to return the read value (because the same function pointer cannot point to functions returning different types). I chose to return the value through a pointer to <code>void</code> so that such function can also be &quot;overloaded&quot; with C11 <code>_Generic()</code> and thus casting and writing into different types for the output.</p>
  <p>Of course, invoking a pointer to a function has a performance overhead, and I guess it's far heavier that checking the value of an <code>enum</code>, but at some point, if the number of tags is big, I believe it would be faster than <code>switch/case</code>.</p>
  <p>My question is: Have you ever seen this technique used somewhere? (I haven't, and I don't know if it's because real world application of this would require <code>_Generic()</code> on the readval function, which requires C11, or if it's because of some problem I'm not noticing at this moment). How many tags do you guess you'd need  to have for the pointer to function being faster -in current Intel CPUs- than the <code>switch/case</code>?</p>