<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 323</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="323"></A><H4>323.
  
Where must <TT>export</TT> appear?
</H4>
<B>Section: </B>Clause 13&#160; [<A href="https://wg21.link/temp">temp</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Daveed Vandevoorde
 &#160;&#160;&#160;

 <B>Date: </B>14 Nov 2001<BR>


<P>[Voted into WP at April 2003 meeting.]</P>



<P>The standard doesn't seem to describe whether the keyword <TT>export</TT>
should appear on exported template declarations that are not used or
defined in that particular translation unit.</P>

<P>For example:</P>
<PRE>
  // File 1:
  template&lt;typename T&gt; void f();  // export omitted

  // File 2:
  export template&lt;typename T&gt; void f() {}

  int main() { f&lt;int&gt;(); }
</PRE>
<P>Another example is:</P>
<PRE>
  // File 1:
  struct S {
     template&lt;typename T&gt; void m();
  };

  // File 2:
  struct S {
     template&lt;typename T&gt; void m();
  };

  export template&lt;typename T&gt; void S::m() {}

  int main() {
     S s;
     S.m&lt;int&gt;();
  }
</PRE>

<P>I think both examples should be clarified to be invalid.  If a template is
exported in one translation unit, it should be declared export in all
translation units in which it appears.</P>

<P>With the current wording, it seems that even the following is valid:</P>
<PRE>
  // File 1:
  export template&lt;typename T&gt; void f();  // export effectively ignored

  // File 2:
  template&lt;typename T&gt; void f() {}  // Inclusion model
  void g() { f&lt;int&gt;(); }

  // File 3:
  void g();
  template&lt;typename T&gt; void f() {}  // Inclusion model

  int main() {
     g();
     f&lt;int&gt;();
  }
</PRE>

<P>In fact, I think the declaration in "File 1" could be a definition and this
would still satisfy the the requirements of the standard, which definitely
seems wrong.</P>

<P><B>Proposed Resolution (revised October 2002):</B></P>

<P>Replace Clause 13 [<A href="https://wg21.link/temp">temp</A>] paragraphs 6, 7, and 8 by the
following text:</P>
<BLOCKQUOTE>
<P>
A <I>template-declaration</I> may be preceded by the <TT>export</TT> keyword.
Such a template is said to be <I>exported</I>.
Declaring exported a class template is equivalent to declaring
exported all of its non-inline member functions, static data members,
member classes, member class templates, and non-inline member function
templates.</P>
<P> If a template is exported in one translation unit, it
shall be exported in all translation units in which
it appears; no diagnostic is required.
A declaration of an exported template shall appear with the <TT>export</TT>
keyword before any point of instantiation (13.8.4.1 [<A href="https://wg21.link/temp.point">temp.point</A>])
of that template in that translation unit.  In addition, the first
declaration of an exported template containing the <TT>export</TT> keyword
must not follow the definition of that template.  The <TT>export</TT>
keyword shall not be used in a friend declaration.</P>
<P> Templates defined in an unnamed namespace, inline functions, and
inline function templates shall not be exported.
An exported non-class template shall be defined only once in a program;
no diagnostic is required. An exported non-class template
need only be declared (and not necessarily defined) in a translation
unit in which it is instantiated.</P>
<P>A non-exported non-class template must be defined in every translation unit
in which it is implicitly instantiated (13.9.2 [<A href="https://wg21.link/temp.inst">temp.inst</A>]),
unless the corresponding
specialization is explicitly instantiated (13.9.3 [<A href="https://wg21.link/temp.explicit">temp.explicit</A>])
in some translation unit; no diagnostic is required.</P>
</BLOCKQUOTE>

<P> Note: This change also resolves issues <A HREF="204.html">204</A>
and <A HREF="335.html">335</A>.</P>

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