title: Use of typename keyword with typedef and new
body: <p>Consider this code,</p>
  <pre><code>template&lt;class T&gt;
  struct Sample
  { 
       typename T::X *x; //declare pointer to T's X
  };
  </code></pre>
  <p>In the above code, the keyword <code>typename</code> is required by the compiler, so that it can disambiguate between nested types and nested values in templates. That means, in the absence of <code>typename</code> keyword, compiler would interpret this as multiplication of T::X with x,</p>
  <pre><code>T::X *x; //multiply T::X with x
  </code></pre>
  <p>So in such situations where ambiguity can arise, the keyword <code>typename</code> becomes <strong>necessity</strong> so as to remove ambiguities. But there are few situations when the context itself removes ambiguities. The <a href="https://stackoverflow.com/questions/4347730/use-of-typename-keyword-with-template-function-parameters">other topic</a> discusses contexts of base-class and function-parameters (the latter doesn't remove ambiguity though). In this topic, I particularly want to discuss other two contexts which seem to be <strong>unambiguous</strong>, but we're still required to write <code>typename</code>,</p>
  <pre><code>typedef typename T::X xtype;
  pX = new typename T::X;  
  </code></pre>
  <p>In these two situations, the keywords <code>typedef</code> and <code>new</code> make it clear enough to the compiler that whatever follows is <em>type</em>, <strong>not</strong> <em>value</em>.</p>
  <p>So my question is, why do compilers still need the <code>typename</code> keyword, even in unambiguous situations such as when we use <code>typedef</code> and <code>new</code>?</p>
  <hr />
  <h1>EDIT (after reading this response from <a href="https://stackoverflow.com/users/34509/johannes-schaub-litb">Johannes Schaub</a>) :</h1>
  <pre><code>//typedef NOT followed by a type!
  int typedef A;
  </code></pre>
  <p>This syntax asks me to modify my question a little bit, so that the point which I'm trying to make, may be seen by others.</p>
  <p>Consider this,</p>
  <pre><code>T::X typedef *x;
  </code></pre>
  <p>So from the context, it's still clear enough to the compiler that T::X is a type, no matter whether it appears <em>before</em> <code>typedef</code>,or <em>after</em> <code>typedef</code>. <strong>Unless C++ allows us to write <code>typedef 5 five</code>  or <code>typedef T::value t_value</code> (where T::value is <em>value</em>), the presence of <code>typedef</code> itself removes all ambiguities and so, <code>typename</code> seems to be an unnecessary requirement by the Standard (in such situations)</strong>. Same argument holds true for <code>new</code> as well.</p>
  <hr />
  <p>Also, I've written a class template which is using this struct as template argument:</p>
  <pre><code>struct A 
  {
          struct X { string name; };
          static const int X = 100;
  };
  </code></pre>
  <p>I particularly want to know if the following code (from the constructor) is correct (portable) or not,</p>
  <pre><code>//two interesting statements
   pX = new typename T::X; //T::X means struct X
   product = T::X * p; //but here, T::X means int X
  </code></pre>
  <p>The complete code is <a href="http://www.ideone.com/ZkAIR" rel="noreferrer">here</a> at ideone. Please have a look at it before replying. :-)</p>