title: Optimal way to access std::tuple element in runtime by index
body: <p>I have function <code>at</code> designed to access std::tuple element by index specified in runtime</p>
  
  <pre><code>template&lt;std::size_t _Index = 0, typename _Tuple, typename _Function&gt;
  inline typename std::enable_if&lt;_Index == std::tuple_size&lt;_Tuple&gt;::value, void&gt;::type
  for_each(_Tuple &amp;, _Function)
  {}
  
  template&lt;std::size_t _Index = 0, typename _Tuple, typename _Function&gt;
  inline typename std::enable_if &lt; _Index &lt; std::tuple_size&lt;_Tuple&gt;::value, void&gt;::type
      for_each(_Tuple &amp;t, _Function f)
  {
      f(std::get&lt;_Index&gt;(t));
      for_each&lt;_Index + 1, _Tuple, _Function&gt;(t, f);
  }
  
  namespace detail { namespace at {
  
  template &lt; typename _Function &gt;
  struct helper
  {
      inline helper(size_t index_, _Function f_) : index(index_), f(f_), count(0) {}
  
      template &lt; typename _Arg &gt;
      void operator()(_Arg &amp;arg_) const
      {
          if(index == count++)
              f(arg_);
      }
  
      const size_t index;
      mutable size_t count;
      _Function f;
  };
  
  }} // end of namespace detail
  
  template &lt; typename _Tuple, typename _Function &gt;
  void at(_Tuple &amp;t, size_t index_, _Function f)
  {
      if(std::tuple_size&lt;_Tuple&gt; ::value &lt;= index_)
          throw std::out_of_range("");
  
      for_each(t, detail::at::helper&lt;_Function&gt;(index_, f));
  }
  </code></pre>
  
  <p>It has linear complexity. How can i achive O(1) complexity?</p>