<HTML>
<HEAD>
   <style>
     <!--
       P.indented {margin-left: 4em}
       XMP.indented {margin-left: 4em}
     -->
   </style>
   <TITLE>
       Syntactic Disambiguation Using the Template Keyword
   </TITLE>
</HEAD>

<BODY>

<TABLE ALIGN=RIGHT CELLSPACING=0 CELLPADDING=0 >
<TR>
<TD ALIGN=RIGHT>Document number:</TD>

<TD>&nbsp;03-0111/N1528</TD>
</TR>

<TR>
<TD ALIGN=RIGHT>Date:</TD>

<TD>&nbsp;September 19, 2003</TD>
</TR>

<TR>
<TD ALIGN=RIGHT>Author:</TD>

<TD>&nbsp;John Spicer, Edison Design Group</TD>
</TR>

<TR>
<TD></TD>

<TD>&nbsp;<TT>jhs@edg.com</TT></TD>
</TR>
</TABLE>

<BR CLEAR=ALL>

<CENTER>
<H2>
Syntactic Disambiguation Using the Template Keyword
</H2>
</center>

<h3>Discussion</h3>
<p>

This is a revision to issue 96 in the core issues list and a follow-up to
an earlier discussion of the issue in N1231.
<p>
In my original reflector posting on this subject I recommended that
the standard be changed to require that the <code>template</code>
keyword be followed by a <i>template-id</i> (i.e., a name followed by
a <i>template-argument-list</i>).
<p>
The following example gives the rationale for this recommendation.
The <code>template</code> keyword is only supposed to provide syntactic
disambiguation, not affect name lookup (just as is the case with the
<code>typename</code> keyword).  This results in the surprising behavior
that call #2 does <em>not</em> call the template function.

<xmp class=indented>
struct A {
	void f(int);
	template <class T> void f(T);
};

template <class T> void f(T t)
{
	A a;
	a.template f<>(t); // #1 calls template
	a.template f(t);   // #2 but does this call the template?
}
</xmp>

The counter example came up while we were implementing template
template parameters.  In this example, the <code>template</code> keyword
is needed in the default argument to indicate that <code>T::C</code> is
a template, just as <code>typename</code> would be required if
<code>T::C</code> were a type.

<xmp class=indented>
template <class T> struct A {template <class T2> struct C {}; };
template <class T, template <class X> class TT = T::template C> struct B {};
</xmp>

In other words, there are cases where we need to permit the
<code>template</code> keyword without a following <code>template-id</code>.
<p>
If we allow the template keyword to be followed by a name without a
<i>template-argument-list</i> we must then decide what it means 
for functions in such cases (i.e., we must resolve the issue illustrated
by the first example above).  For classes it is not an issue
(if the name is followed by a <i>template-argument-list</i>, it refers
to a specialization of the class template, if not, it refers to the
class template itself).
<p>
There are two possible interpretations that I can think of:
<ol>
<li>
When the <code>template</code> keyword is applied to an overload set
containing both template and non-templates, the non-templates are ignored.
In other words, it is treated as if the name were followed by an
empty <i>template-argument-list</i> (i.e., <code>&lt;></code>).
<li>
If the <code>template</code> keyword is followed by a name that does
not have a <i>template-argument-list</i>, the name must refer to
a class template and be used in a context in which a class template
is valid (i.e., the example above would be ill-formed).
</ol>
Note that a name that refers to a class template (and not to a specialization
of the class template) must be a template
argument associated with a template template parameter, because there is
no other context in which a template name without a
<i>template-argument-list</i> is permitted).

<p>
Existing practice seems to favor #2.  The Microsoft, g++, and EDG compilers
all accept <code>a.template f<>(t)</code>, and all reject
<code>a.template f(t)</code>.


<p>
When this was last discussed, a few issues were raised:

<ol>
<li>
How does the the <code>template</code> keyword interact with
using-declarations.
<li>
Is it really necessary to use <code>template</code> when naming a
dependent template template argument?
</ol>

<p>
The first issue has now been dealt with by issue #109 (<code>template</code>
is not permitted before the final component of a qualified-name in
a using-declaration).

<p>
As for the second issue, the <code>template</code> keyword is not
required in order for syntactic purposes, so in theory it would be
possible to not require its use when naming a dependent template template
argument.  However, the same statement could be made about certain uses
of <code>typename</code> that are not strictly required.

<h3>Recommendations</h3>

<p>
I've personally gone back and forth on the issue of permitting
<code>template</code> in a class member access that does not include a
template argument list.  My summary of the strongest arguments on both
sides are:

<ul>
<li>
Pro: In general, we have been loosening restrictions about the placement
of <code>typename</code> and <code>template</code> to make their use
simpler.  It would be nice if we could allow <code>template</code>
in places where it is not required to make life easier on less sophisticated
users.
<li>
Con: If <code>template</code> were to affect the set of names considered
by overload resolution in a reference like "<code>p->template f()</code>",
the keyword would have subtle semantic effects, something that we've
been avoiding for both <code>typename</code> and <code>template</code>.
<p>
What looks like an advantage to the user (being able to liberally use
<code>template</code>) can actually have surprising semantic consequences.
In other words, both of these would be valid:

<xmp class=indented>
p->template f();
p->f();
</xmp>

But they would potentially call different functions.  Contrast this with
the alternate rule in which one call would be valid and the other ill-formed:

<xmp class=indented>
p->template f<>();
p->f<>();
</xmp>
</ul>

<p>
While I would like to make use of <code>template</code> easier for users,
I think the cost of understanding the subtle semantic impact outweighs
any advantages.

<p>
Consequently, the recommendation of this paper is:
If the <code>template</code> keyword is followed by a name that does
not have a <i>template-argument-list</i>, the name must refer to
a class template and be used in a context in which a class template
is valid, otherwise the program is ill-formed.

<p>
<h3>End of document.</h3>

</body>
