title: Type sensitive tuple visitor
body: <p>Suppose I have a <code>std::tuple</code> made up of types like</p>
  
  <pre><code>struct A {
    static void tip();
  };
  
  struct B {
    static void tip();
  };
  
  struct Z {
  };
  
  std::tuple&lt;const A&amp;,const B&amp;,const Z&amp;&gt; tpl;
  </code></pre>
  
  <p>Yes, I need separate <code>A</code>, <code>B</code>. (The implementation of <code>::tip()</code> differs for each type.) What I try to implement is a type-sensitive "visitor" that iterates through the tuple starting from the beginning to the end. Upon visiting a particular element of type <code>T</code> a function should be called depending on whether <code>T</code> has the <code>::tip()</code> method or not. In the simple example of above only <code>A</code> and <code>B</code> have <code>::tip()</code> implemented and <code>Z</code> not. So, the iterator should call twice the function for types with the <code>::tip()</code> method and once the other function.</p>
  
  <p>Here is what I came up with:</p>
  
  <pre><code>template&lt; int N , bool end &gt;
  struct TupleIter
  {
    template&lt; typename T , typename... Ts &gt;
    typename std::enable_if&lt; std::is_function&lt; typename T::tip &gt;::value , void &gt;::type
    static Iter( const T&amp; dummy , const std::tuple&lt;Ts...&gt;&amp; tpl ) {
      std::cout &lt;&lt; "tip\n";
      std::get&lt;N&gt;(tpl); // do the work
      TupleIter&lt;N+1,sizeof...(Ts) == N+1&gt;::Iter( std::get&lt;N+1&gt;(tpl) , tpl );
    }
  
    template&lt; typename T , typename... Ts &gt;
    typename std::enable_if&lt; ! std::is_function&lt; typename T::tip &gt;::value , void &gt;::type
    static Iter( const T&amp; dummy , const std::tuple&lt;Ts...&gt;&amp; tpl ) {
      std::cout &lt;&lt; "no tip\n";
      std::get&lt;N&gt;(tpl); // do the work
      TupleIter&lt;N+1,sizeof...(Ts) == N+1&gt;::Iter( std::get&lt;N+1&gt;(tpl) , tpl );
    }
  };
  
  
  template&lt; int N &gt;
  struct TupleIter&lt;N,true&gt;
  {
    template&lt; typename T , typename... Ts &gt;
    static void Iter( const std::tuple&lt;Ts...&gt;&amp; tpl ) {
      std::cout &lt;&lt; "end\n";
    }
  };
  </code></pre>
  
  <p>I use a <code>dummy</code> instance of the type of the element at the iterator position and decide via <code>enable_if</code> which function to call. Unfortunately this doesn't work/isn't a nice solution:</p>
  
  <ol>
  <li>The compiler complains about recursive instantiation</li>
  <li>The <code>const T&amp; dummy</code> is not a clean solution</li>
  </ol>
  
  <p>I was wondering if <code>enable_if</code> is the right strategy to do the decision and how can one recursively iterate through the <code>std::tuple</code> capturing the first type and keeping all the remaining arguments in vital state. Read through <a href="https://stackoverflow.com/questions/10626856/how-to-split-a-tuple">How to split a tuple?</a> but it doesn't do any decision.</p>
  
  <p>How can one implement such a thing in a correct and portable way in C++11?</p>