title: Template argument deduction from function body
body: <p>If we've this function template,</p>
  
  <pre><code>template&lt;typename T&gt;
  void f(T param) {}
  </code></pre>
  
  <p>Then we can call it in the following ways,</p>
  
  <pre><code>int i=0;
  f&lt;int&gt;(i);//T=int : no need to deduce T
  f(i); //T=int : deduced T from the function argument!
  
  //likewise
  sample s;
  f(s); //T=sample : deduced T from the function argument!
  </code></pre>
  
  <p>Now consider this variant of the above function template,</p>
  
  <pre><code>template&lt;typename TArg, typename TBody&gt;
  void g(TArg param) 
  {
     TBody v=param.member;
  }
  </code></pre>
  
  <p>Now, can the compiler deduce the template arguments if we write,</p>
  
  <pre><code>sample s;
  g(s); //TArg=sample, TBody=int??
  </code></pre>
  
  <p>Suppose <code>sample</code> is defined as,</p>
  
  <pre><code>struct sample
  {
     int member;
  };
  </code></pre>
  
  <hr>
  
  <p>There are basically two questions:</p>
  
  <ul>
  <li>Can the compiler deduce the template arguments in the second example?</li>
  <li>If no, then why? Is there any difficulty? If the Standard doesn't say anything about <em>"template argument deduction from  function body"</em>, then is it because the argument(s) cannot be deduced? Or it didn't consider such deduction so as to avoid adding more complexity to the language? Or what?</li>
  </ul>
  
  <p>I would like know your views on such deduction.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>By the way, the GCC is able to deduce function arguments if we write this code:</p>
  
  <pre><code>template&lt;typename T&gt;
  void h(T p)
  {
          cout &lt;&lt; "g() " &lt;&lt; p &lt;&lt; endl;
          return;
  }
  template&lt;typename T&gt;
  void g(T p)
  {
          h(p.member); //if here GCC can deduce T for h(), then why not TBody in the previous example?
          return;
  }
  </code></pre>
  
  <p>Working demonstration for this example : <a href="http://www.ideone.com/cvXEA" rel="nofollow">http://www.ideone.com/cvXEA</a></p>
  
  <p>Not working demonstration for the previous example: <a href="http://www.ideone.com/UX038" rel="nofollow">http://www.ideone.com/UX038</a></p>