title: Can you make a computed goto in C++?
body: <p><a href="https://en.wikipedia.org/wiki/Fortran" rel="nofollow noreferrer">Fortran</a> has a computationally efficient way called a 'computed goto'. The construct uses an index into a branch table to perform a direct goto. If I remember correctly the syntax is:</p>
  <pre class="lang-none prettyprint-override"><code>go to index (label1, label2, ...)
  </code></pre>
  <p>where the index is used to reference a code pointer (label) in the parenthesized list.</p>
  <p>I have a case where a computed goto is a better solution than a switch statement and would like to construct one, but I can't figure out how.</p>
  <p>Now before the jibes and slings arrive, it is possible for the compiler to optimize a computed goto, but I have no guarantee that it will.</p>
  <hr />
  <p>A switch statement can always be used. In some cases a switch statement can be optimized to a jump table (the implementation of a computed goto).</p>
  <p>However, this is only possible when the range of case values is an almost dense covering (there is almost a case statement for each integer in the range of low values to high values). When this is not the case, the implementation will probably be a binary tree. The compiler writer has a choice to optimize to a jump table when appropriate or not. Where a binary tree will always satisfy the semantics of a switch statement, where sometimes a jump table is sufficient lets me ask whether I can guarantee a jump table when appropriate. I have no control over the compiler writer.</p>
  <p>As a simple case, I often write lexers (<a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="nofollow noreferrer">FSMs</a>), and I use three data constructs, one to map the input into an acceptable alphabet, one to perform node transitions, and one to execute some code based on the current state and the input value. The implementation of the FSM is a <a href="https://en.wikipedia.org/wiki/Mealy_machine" rel="nofollow noreferrer">Mealy machine</a>, not a <a href="https://en.wikipedia.org/wiki/Moore_machine" rel="nofollow noreferrer">Moore machine</a>, so actions are performed on arcs (transitions) and not on nodes.</p>
  <p>The actions performed are typically small, often no more than a line of source code. I recognize that functions can be used, and that their use removes the need for a jump table. But I believe that I can not 'point' to an inline function, and therefore functions are closed-form callable procedures.</p>
  <p>This is less efficient in most cases, than a switch statement with or without jump table optimization. If I can use a jump table then I avoid the issue of what the compiler writer thinks of optimization and am able to write efficient code.</p>
  <p>As to the general case brought up below about the issues related to a Fortran computed goto: This is not a criticism of that/those comment. But the qualitative issues, even though they are true, does not answer the question.</p>
  <p>There is an answer below using <code>void* &amp;&amp;label;</code> and I'd like to thank you for that. But, alas, as you pointed out this is non-standard C/C++ and is likely not to be present in the future. So, it's better not to do this.</p>
  <p>I hope that I've answered the 'get a better compiler' comment. And I hope I've at least addressed the issues with using function pointers. And at last, this was a moment of curiosity for me. I didn't think that I should provide the germicidal history of why I thought the question had some carrying power. But now I know. Whenever, and I mean whenever, I write to this group I better tell you what all my duckies are so that they can be shot down good and proper.</p>