title: Verify bug in GCC
body: <p>I'd like to verify that the following is a bug in GCC and not in my understanding of C++. Consider the following code:</p>
  
  <pre><code>struct A
  {
      struct B
      {
          template&lt; typename U &gt; U as() const { return U(); }
      };
  
      B operator[]( int ) const { return B(); }
  };
  
  template&lt; typename T &gt;
  struct as
  {
      template&lt; typename U &gt;
      static T call( const U&amp; u )
      {
          return u[ 0 ].as&lt; T &gt;(); // accepted by Clang 3.2, rejected by GCC 4.7
          // return u[ 0 ].template as&lt; T &gt;(); // does not help and is IMHO not needed
          // return u[ 0 ].A::B::as&lt; T &gt;(); // accepted by GCC 4.7
      }
  };
  
  int main()
  {
      as&lt; int &gt;::call( A() );
  }
  </code></pre>
  
  <p>IMHO the code should be fine, it is accepted by Clang 3.2, but not by GCC 4.7 (4.4 and 4.6 also fail with basically the same error but 4.4 produces a slightly different message). Here's the output from my shell:</p>
  
  <pre><code>$ clang++-3.2 -O3 -Wall -Wextra -std=c++0x t.cc -o t
  $ g++-4.7 -O3 -Wall -Wextra -std=c++0x t.cc -o t
  t.cc: In static member function ‘static T as&lt;T&gt;::call(const U&amp;)’:
  t.cc:17:21: error: invalid use of ‘struct as&lt;T&gt;’
  t.cc: In static member function ‘static T as&lt;T&gt;::call(const U&amp;) [with U = A; T = int]’:
  t.cc:18:4: warning: control reaches end of non-void function [-Wreturn-type]
  $ 
  </code></pre>
  
  <p><strong>Question:</strong> Is this a bug in GCC or am I missing something?</p>
  
  <p><strong>EDIT:</strong> I'm a bit confused: The GCC bug report at <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55576" rel="nofollow">http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55576</a> says at comment #9 that the code in comment #3 is "valid". What exactly does this mean? It looks like the GCC folks think that it actually <em>is</em> a bug, otherwise they would have closed it already? OTOH the answer from @Potatoswatter seems to be quite clear that it should be correct and I should file a bug report against Clang (or is there already such a bug report?)</p>
  
  <p>Note that I hesitate to mark an answer as accepted until the above is clarified. Since both answers are already helpful (one to explain, one to work-around), I gave both an upvote.</p>
  
  <p>Bonus question: Since I got a downvote for non-narrative code, I wonder how others feel. I tried to create a SCCEE which removes all distractions and concentrates on the technical issue. That's how I prefer thinking about these things. Is that wrong?</p>
  
  <p>Also, @EdHeal: Why is the code prone to disaster? (You don't think that is the real-world code I have, right?)</p>
  
  <p><strong>EDIT2:</strong> Thanks, David, just noticed your edit. I'll mark your answer as accepted now and I also saw that you commented on the GCC bug report. I think the main point of this question is thereby answered and GCC got another reminder. Thanks everyone.</p>