<div class="gmail_quote">On Fri, Jul 8, 2011 at 4:49 PM, Steven Schveighoffer <span dir="ltr"><<a href="mailto:schveiguy@yahoo.com">schveiguy@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
On Fri, 08 Jul 2011 11:08:14 -0400, James Fisher <<a href="mailto:jameshfisher@gmail.com" target="_blank">jameshfisher@gmail.com</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
On Fri, Jul 8, 2011 at 4:06 PM, James Fisher <<a href="mailto:jameshfisher@gmail.com" target="_blank">jameshfisher@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
So ... stuff works, but I'm not really sure why one uses function<br>
templating and the other uses return type inference.  Any answers?<br>
</blockquote>
<br>
<br></div><div class="im">
... Wait, brain malfunction; I think I do understand.  The type is inferable<br>
purely from the function definition in the latter case.<br>
</div></blockquote>
<br>
<br>
Yes, the return type is determined by the return statement in the function itself.  Note that it would be an error to have two different return types that compiled into the same function.  A frequent use of auto is when the return type could be different based on template parameters to the function, or version statements.  In these cases, effectively there is only one return type, because only one return statement gets compiled.<br>

<br>
The reason your getSquares(T)() *call* doesn't work is because the compiler first must instantiate the template, and it doesn't have enough information to do so.  Remember that a function template like this:<br>
<br>
T getSquares(T)() {...}<br>
<br>
is equivalent to this:<br>
<br>
template getSquares(T)<br>
{<br>
   T getSquares() {...}<br>
}<br>
<br>
So in fact, the compiler cannot evaluate the inner part of the template until it knows what T is.  However, there is a special feature called implicit function template instantiation (IFTI) which allows the compiler to infer the values of T from the function call being made.  This means the types of arguments to the function can be used to infer the template parameters.  This *only* works for template functions or templates that can be reduced to template functions.<br>

<br>
So for example:<br>
<br>
T foo(T)(T t) {return t;}<br>
<br>
you can call this function via:<br>
<br>
foo(1);<br>
<br>
And T will implicitly be assigned int.<br>
<br>
But since your getSquares function takes no parameters, IFTI cannot be used.  Note that the return type does NOT play a role in IFTI.<br>
<br>
However, you can still use getSquares, just specify the template parameter explicitly:<br>
<br>
auto sq = getSquares!int();<br>
<br>
Another option is to give a default value for T:<br>
<br>
T getSquares(T = int)() {...}<br>
<br>
-Steve<br>
</blockquote></div><br><div>This all makes sense.  Thanks for the comprehensive explanation. :)</div>