title: decltype and the scope operator in C++
body: <p>I need to obtain the type which was supplied when instantiating a template. Consider the following example:</p>
  
  <pre><code>template &lt;typename T&gt; struct Foo
  {
    typedef T TUnderlying;
  };
  
  static Foo&lt;int&gt; FooInt;
  
  class Bar
  {
  public:
    auto Automatic() -&gt; decltype(FooInt)::TUnderlying
    {
      return decltype(FooInt)::TUnderlying();
    }
  };
  
  int main()
  {
    Bar bar;
    auto v = bar.Automatic();
      return 0;
  }
  </code></pre>
  
  <p>Problem with this code is using the scope operator together with decltype. Visual C++ 2010 complains like this:</p>
  
  <p>error C2039: 'TUnderlying' : is not a member of '`global namespace''</p>
  
  <p>I gathered some information on the topic on Wikipedia:</p>
  
  <p>While commenting on the formal Committee Draft for C++0x, the Japanese ISO member body noted that "a scope operator(::) cannot be applied to decltype, but it should be. It would be useful in the case to obtain member type(nested-type) from an instance as follows":[16]</p>
  
  <pre><code>vector&lt;int&gt; v;
  decltype(v)::value_type i = 0; // int i = 0;
  </code></pre>
  
  <p>This, and similar issues were addressed by David Vandevoorde, and voted into the working paper in March 2010.</p>
  
  <p>So I reckon the Visual C++ 2010 does not have this implemented. I came up with this workaround:</p>
  
  <pre><code>template &lt;typename T&gt; struct ScopeOperatorWorkaroundWrapper
  {
    typedef typename T::TUnderlying TTypedeffedUnderlying;
  };
  
  auto Automatic() -&gt; ScopeOperatorWorkaroundWrapper&lt;decltype(FooInt)&gt;::TTypedeffedUnderlying
  {
    return ScopeOperatorWorkaroundWrapper&lt;decltype(FooInt)&gt;::TTypedeffedUnderlying();
  }
  </code></pre>
  
  <p>Did I miss any solution which is more elegant and less verbose?</p>