title: Using boost mp11 to switch on runtime value efficiently(breaking when processing function is done)
body: <p>I have the following <a href="https://godbolt.org/z/K54b8d" rel="nofollow noreferrer">code</a> where I implement dispatching on runtime value to interpret the data in certain way(in this toy example data can be either uint8_t or short).</p>
  <p>Code seems to work, but I am wondering if I can somehow microoptimize the code so that when I have a hit(processing function matches) processing is stopped (currently even if first element of tuple is a &quot;handler&quot; entire tuple is iterated over at runtime).</p>
  <pre><code>#include &lt;boost/mp11/tuple.hpp&gt;
  #include &lt;iostream&gt;
  
  uint8_t data[4] = {0,1,100,2};
  
  template&lt;int runtimeId, typename T&gt;
  struct kindToType{
      static constexpr int id =  runtimeId;
      using type = T;
  };
  
   const auto print  =[]&lt;typename T&gt; (const T* data){
      if constexpr(std::is_same_v&lt;short, std::remove_cvref_t&lt;T&gt;&gt;){
          const short* values = (const short*)data;
          std::cout &lt;&lt; values[0] &lt;&lt; &quot;  &quot; &lt;&lt; values[1] &lt;&lt; std::endl;
      } else if constexpr(std::is_same_v&lt;uint8_t, std::remove_cvref_t&lt;T&gt;&gt;){
          const uint8_t* values = (const uint8_t*)data;
      std::cout &lt;&lt; (int)values[0] &lt;&lt; &quot;  &quot; &lt;&lt; (int)values[1]&lt;&lt; &quot;  &quot; &lt;&lt; (int)values[2] &lt;&lt; &quot;  &quot; &lt;&lt; (int)values[3] &lt;&lt; std::endl;;
      }
  };
  
  static constexpr std::tuple&lt;kindToType&lt;10, uint8_t&gt;, kindToType&lt;11, short&gt;&gt; mappings{};
  
  void dispatch(int kind){
      boost::mp11::tuple_for_each(mappings, [kind]&lt;typename Mapping&gt;(const Mapping&amp;) {
      if (Mapping::id == kind)
      {
          print((typename Mapping::type*)data);
      }
      });
  }
  int main()
  {
      // no guarantee that kind is index like(e.g. for two values
      // it can have values 47 and 1701)
      dispatch(10);
      dispatch(11);
  }
  </code></pre>
  <p>Notes:</p>
  <ul>
  <li>I can not/want to use std::variant.</li>
  <li>I do not want to use std::map or std::unordered map(where value is <code>std::function</code>)</li>
  <li>I know this is premature optimization(even 10 integer comparisons is cheap assuming handlers do nontrivial amount of work).</li>
  <li>my handlers are unique, i.e. it is std::map like thing, not std::multimap like thing so it is fine to <code>break;</code>.</li>
  <li>kind of id used for runtime values is not guaranteed to have values in [0, n-1].</li>
  <li>I am fine with C++20 solution as long as it is implemented in at least 1 compiler.</li>
  </ul>