title: Portable branch prediction hint in c++
body: <p>Branches prediction have been addressed a couple of time on StackOverflow. However, I didn't specifically found the answer to what I am looking for.<br>
  During the optimization phase, I need to avoid branch misprediction. And I have a number of verification that I need to do. It looks like:</p>
  
  <pre><code>if(!successCondition)
      { throw Something(); }
  </code></pre>
  
  <p>Of course, in the normal expected workflow, which happen most of the cases, we don't throw exception, thus we don't enter the if.</p>
  
  <p>I know that in the common if/else paradigm, we can hint the compiler by putting the most likely branch in the if, and the less likely in the else (<a href="https://stackoverflow.com/questions/3702903/portable-branch-prediction-hints#3702965">Portable branch prediction hints</a>). But I do not want (because of the readability) chain the ifs:</p>
  
  <pre><code>if(successCondition)
      { whatever(); }
  else
      { throw Something(); }
  </code></pre>
  
  <p>Thus, I understand that the compiler will, by default, favor the entry in the if and I will get a branch misprediction.</p>
  
  <p>I know that gcc has a specific function to optimize the code, which is called with unlikely in the Linux kernel (<a href="https://stackoverflow.com/questions/32581644/branch-aware-programming">Branch-aware programming</a>). But it is not portable, and I need my code to be.</p>
  
  <p>Is there a way to have the correct branch prediction in C++ that remains portable?</p>