<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 275</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<STYLE TYPE="text/css">
  INS { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .INS { text-decoration:none; background-color:#D0FFD0 }
  DEL { text-decoration:line-through; background-color:#FFA0A0 }
  .DEL { text-decoration:line-through; background-color: #FFD0D0 }
  @media (prefers-color-scheme: dark) {
    HTML { background-color:#202020; color:#f0f0f0; }
    A { color:#5bc0ff; }
    A:visited { color:#c6a8ff; }
    A:hover, a:focus { color:#afd7ff; }
    INS { background-color:#033a16; color:#aff5b4; }
    .INS { background-color: #033a16; }
    DEL { background-color:#67060c; color:#ffdcd7; }
    .DEL { background-color:#67060c; }
  }
  SPAN.cmnt { font-family:Times; font-style:italic }
</STYLE>
</HEAD>
<BODY>
<P><EM>This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21
  Core Issues List revision 118b.
  See http://www.open-std.org/jtc1/sc22/wg21/ for the official
  list.</EM></P>
<P>2025-09-28</P>
<HR>
<A NAME="275"></A><H4>275.
  
Explicit instantiation/specialization and <I>using-directive</I>s
</H4>
<B>Section: </B>13.9.4&#160; [<A href="https://wg21.link/temp.expl.spec">temp.expl.spec</A>]
 &#160;&#160;&#160;

 <B>Status: </B>CD1
 &#160;&#160;&#160;

 <B>Submitter: </B>John Spicer
 &#160;&#160;&#160;

 <B>Date: </B>15 Feb 2001<BR>


<P>[Moved to DR at 4/02 meeting.]</P>



<P>Consider this example:</P>

<PRE>
    namespace N {
	template &lt;class T&gt; void f(T){}
	template &lt;class T&gt; void g(T){}
	template &lt;&gt; void f(int);
	template &lt;&gt; void f(char);
	template &lt;&gt; void g(char);
    }

    using namespace N;

    namespace M {
	template &lt;&gt; void N::f(char){}  // prohibited by standard
	template &lt;class T&gt; void g(T){}
	template &lt;&gt; void g(char){}     // specialization of M::g or ambiguous?
	template void f(long);         // instantiation of N::f?
    }

    template &lt;class T&gt; void g(T){}

    template &lt;&gt; void N::f(char){}  // okay
    template &lt;&gt; void f(int){}      // is this a valid specialization of N::f?

    template void g(int);          // instantiation of ::g(int) or ambiguous?
</PRE>

<P>The question here is whether unqualified names made visible by a
<I>using-directive</I> can be used as the declarator in an explicit
instantiation or explicit specialization.</P>

<P>Note that this question is already answered for qualified names in
9.3.4 [<A href="https://wg21.link/dcl.meaning#1">dcl.meaning</A>] paragraph 1.  In a qualified name such as
<TT>N::f</TT>, <TT>f</TT> must be a member of class or namespace
<TT>N</TT>, not a name made visible in <TT>N</TT> by a
<I>using-directive</I> (or a <I>using-declaration</I>, for that
matter).</P>

<P>The standard does not, as far as I can tell, specify the behavior of
these cases one way or another.</P>

<P>My opinion is that names from <I>using-directive</I>s should not be
considered when looking up the name in an unqualified declarator in an
explicit specialization or explicit instantiation.  In such cases, it
is reasonable to insist that the programmer know exactly which
template is being specialized or instantiated, and that a qualified
name must be used if the template is a member of a namespace.</P>

<P>As the example illustrates, allowing names from
<I>using-directive</I>s to be used would also have the affect of
making ambiguous otherwise valid instantiation and specialization
directives.</P>

<P>Furthermore, permitting names from <I>using-directive</I>s would
require an additional rule to prohibit the explicit instantiation of
an entity in one namespace from being done in another (non-enclosing)
namespace (as in the instantiation of <TT>f</TT> in namespace
<TT>M</TT> in the example).</P>

<P>
<U>Mike Miller</U>: I believe the explicit specialization case
is already covered by _N4868_.9.8.2.3 [<A href="https://wg21.link/namespace.memdef#2">namespace.memdef</A>] paragraph 2,
which requires using a qualified name to define a namespace member
outside its namespace.</P>

<P>
<U>John Spicer</U>: _N4868_.9.8.2.3 [<A href="https://wg21.link/namespace.memdef">namespace.memdef</A>] deals with
namespace members.  An explicit specialization directive deals with
something that is a specialization of a namespace member.  I don't
think the rules in _N4868_.9.8.2.3 [<A href="https://wg21.link/namespace.memdef">namespace.memdef</A>] could be taken to
apply to specializations unless the standard said so explicitly.</P>

<P>
<B>Proposed resolution (suggested 04/01, proposed 10/01)</B>:</P>

<P>(The first change below will need to be revised in accordance
with the resolution of <A HREF="284.html">issue 284</A> to
add a cross-reference to the text dealing with class names.)</P>

<OL>

<LI>
<P>Add in 13.9.3 [<A href="https://wg21.link/temp.explicit#2">temp.explicit</A>] paragraph 2 before the
example:</P>

<BLOCKQUOTE>

An explicit instantiation shall appear in an enclosing namespace
of its template.  If the name declared in the explicit
instantiation is an unqualified name, the explicit instantiation
shall appear in the namespace where its template is declared.
[<I>Note:</I> Regarding qualified names in declarators, see
9.3.4 [<A href="https://wg21.link/dcl.meaning">dcl.meaning</A>].]

</BLOCKQUOTE>

</LI>

<LI>
<P>Change the first sentence of _N4868_.9.8.2.3 [<A href="https://wg21.link/namespace.memdef#1">namespace.memdef</A>] paragraph 1
from</P>

<BLOCKQUOTE>

Members of a namespace can be defined within that namespace.

</BLOCKQUOTE>

<P>to</P>

<BLOCKQUOTE>

Members (including explicit specializations of templates
(13.9.4 [<A href="https://wg21.link/temp.expl.spec">temp.expl.spec</A>])) of a namespace can be defined
within that namespace.

</BLOCKQUOTE>

</LI>

<LI>
<P>Change the first sentence of _N4868_.9.8.2.3 [<A href="https://wg21.link/namespace.memdef#2">namespace.memdef</A>] paragraph 2
from</P>

<BLOCKQUOTE>

Members of a named namespace can also be defined...

</BLOCKQUOTE>

<P>to</P>

<BLOCKQUOTE>

Members (including explicit specializations of templates (13.9.4 [<A href="https://wg21.link/temp.expl.spec">temp.expl.spec</A>])) of a named namespace can also be defined...

</BLOCKQUOTE>

</LI>

<LI>
<P>Change the last sentence of 13.9.4 [<A href="https://wg21.link/temp.expl.spec#2">temp.expl.spec</A>] paragraph 2
from</P>

<BLOCKQUOTE>

If the declaration is not a definition, the specialization may be
defined later in the namespace in which the explicit specialization
was declared, or in a namespace that encloses the one in which the
explicit specialization was declared.

</BLOCKQUOTE>

<P>to</P>

<BLOCKQUOTE>

If the declaration is not a definition, the specialization may be
defined later (_N4868_.9.8.2.3 [<A href="https://wg21.link/namespace.memdef">namespace.memdef</A>]).

</BLOCKQUOTE>

</LI>

</OL>

<BR><BR>
</BODY>
</HTML>
