title: Nested template argument deduction for class templates not working
body: <p>In <a href="https://stackoverflow.com/a/42221591/819272"><strong>this Q&amp;A</strong></a> I wrote a little wrapper class that provides reverse iterator access to a range, relying on the c++1z language feature template argument deduction for class templates (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0091r3.html" rel="nofollow noreferrer"><strong>p0091r3</strong></a>, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0512r0.pdf" rel="nofollow noreferrer"><strong>p0512r0</strong></a>)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iterator&gt;
  #include &lt;vector&gt;
  
  template&lt;class Rng&gt;
  class Reverse
  {
      Rng const&amp; rng;    
  public:    
      Reverse(Rng const&amp; r) noexcept
      : 
          rng(r)
      {}
  
      auto begin() const noexcept { using std::end; return std::make_reverse_iterator(end(rng)); }
      auto end()   const noexcept { using std::begin; return std::make_reverse_iterator(begin(rng)); }
  };
  
  int main()
  {
      std::vector&lt;int&gt; my_stack;
      my_stack.push_back(1);
      my_stack.push_back(2);
      my_stack.puhs_back(3);
  
      // prints 3,2,1
      for (auto const&amp; elem : Reverse(my_stack)) {
          std::cout &lt;&lt; elem &lt;&lt; ',';    
      }
  }
  </code></pre>
  
  <p>However, doing a nested application of <code>Reverse</code> does not yield the original iteration order</p>
  
  <pre><code>// still prints 3,2,1 instead of 1,2,3
  for (auto const&amp; elem : Reverse(Reverse(my_stack))) {
      std::cout &lt;&lt; elem &lt;&lt; ',';    
  }
  </code></pre>
  
  <p><a href="http://melpon.org/wandbox/permlink/xk0yEumtsG1hp6VT" rel="nofollow noreferrer"><strong>Live Example</strong></a> (same output for g++ 7.0 SVN and clang 5.0 SVN)</p>
  
  <p>The culprit seems to be the template argument deduction for class templates because the usual wrapper function does allow for correct nesting</p>
  
  <pre><code>template&lt;class Rng&gt;
  auto MakeReverse(Rng const&amp; rng) { return Reverse&lt;Rng&gt;(rng); }
  
  // prints 1,2,3
  for (auto const&amp; elem : MakeReverse(MakeReverse(my_stack))) {
      std::cout &lt;&lt; elem &lt;&lt; ',';    
  }
  </code></pre>
  
  <p><a href="http://melpon.org/wandbox/permlink/uS9dUBYhWH9Ql4fK" rel="nofollow noreferrer"><strong>Live Example</strong></a> (same output for g++ and clang)</p>
  
  <p><strong>Question</strong>: is nested template argument deduction for class templates supposed to work only "one level" deep, or is this a bug in the current implementations of both g++ and clang?</p>