<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 1378</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="1378"></A><H4>1378.
  
When is an instantiation required?
</H4>
<B>Section: </B>13.9.2&#160; [<A href="https://wg21.link/temp.inst">temp.inst</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Jason Merrill
 &#160;&#160;&#160;

 <B>Date: </B>2011-08-18<BR>


<P>[ Resolved by P0859R0, approved in November, 2017. ]</P>



<P>A template instantiation can be &#8220;required&#8221; without
there being a need for it at link time if it can appear in a
constant expression:</P>

<PRE>
    template &lt;class T&gt; struct A {
       static const T t;
    };
    template &lt;class T&gt; const T A&lt;T&gt;::t = 0;
    template &lt;int I&gt; struct B { };
    int a = sizeof(B&lt;A&lt;int&gt;::t&gt;);

    template &lt;class T&gt; constexpr T f(T t) { return t; }
    int b = sizeof(B&lt;f(42)&gt;);
</PRE>

<P>It seems like it might be useful to define a term other than odr-used
for this sort of use, which is like odr-used but doesn't depend on
potentially evaluated context or lvalue-rvalue conversions.</P>

<P><U>Nikolay Ivchenkov:</U></P>

<P>Another possibility would be to introduce the extension
described in the closed <A HREF="1272.html">issue 1272</A> and
then change 6.3 [<A href="https://wg21.link/basic.def.odr#2">basic.def.odr</A>] paragraph 2 as follows:</P>

<BLOCKQUOTE>

<P>An expression <INS><TT>E</TT></INS> is <I>potentially
evaluated</I> <DEL>unless it is an unevaluated operand (
Clause 7 [<A href="https://wg21.link/expr">expr</A>]) or a subexpression thereof.</DEL>
<INS>if and only if</INS>
</P>

<UL>
<LI><P><INS><TT>E</TT> is a full-expression, or</INS></P></LI>

<LI><P><INS><TT>E</TT> appears in a context where a constant expression
is required, or</INS></P></LI>

<LI><P><INS><TT>E</TT> is a direct subexpression of a potentially-evaluated
expression and <TT>E</TT> is not an unevaluated operand.</INS></P></LI>

</UL>

<P>
<INS>An expression <TT>S</TT> is a <I>direct subexpression</I> of an
expression <TT>E</TT> if and only if <TT>S</TT> and <TT>E</TT>
are different expressions, <TT>S</TT> is a subexpression of
<TT>E</TT>, and there is no expression <TT>X</TT> such that
<TT>X</TT> differs from both <TT>S</TT> and <TT>E</TT>,
<TT>S</TT> is a subexpression of <TT>X</TT>, and <TT>X</TT> is a
subexpression of <TT>E</TT>.</INS> A variable whose name appears
as a potentially-evaluated expression is <I>odr-used</I>
<DEL>unless it is an object that satisfies the requirements for
appearing in a constant expression (7.7 [<A href="https://wg21.link/expr.const">expr.const</A>])
and the lvalue-to-rvalue conversion (4.1) is immediately
applied</DEL>...</P>

<P><INS>[<I>Example:</I></INS></P>

<PRE>
<INS>    template &lt;class T&gt; struct X {
        static int const m = 1;
        static int const n;
    };
    template &lt;class T&gt; int const X&lt;T&gt;::n = 2;

    int main() {
        //<SPAN CLASS="cmnt"> </SPAN>X&lt;void&gt;::m<SPAN CLASS="cmnt"> is odr-used,</SPAN>
        //<SPAN CLASS="cmnt"> </SPAN>X&lt;void&gt;::m<SPAN CLASS="cmnt"> is defined implicitly</SPAN>
        std::cout &lt;&lt; X&lt;void&gt;::m &lt;&lt; std::endl;

        //<SPAN CLASS="cmnt"> </SPAN>X&lt;void&gt;::n<SPAN CLASS="cmnt"> is odr-used,</SPAN>
        //<SPAN CLASS="cmnt"> </SPAN>X&lt;void&gt;::n<SPAN CLASS="cmnt"> is defined explicitly</SPAN>
        std::cout &lt;&lt; X&lt;void&gt;::n &lt;&lt; std::endl;

        //<SPAN CLASS="cmnt"> OK (issue 712 is not relevant here)</SPAN>
        std::cout &lt;&lt; (1 ? X&lt;void&gt;::m : X&lt;void&gt;::n) &lt;&lt; std::endl;
    }
</INS>
</PRE>

</BLOCKQUOTE>

<P>(See also issues <A HREF="712.html">712</A> and
<A HREF="1254.html">1254</A>.)</P>

<P><B>Additional notes (June, 2023)</B></P>

<P>This was addressed by the introduction of "needed for constant
evaluation" in P0859R0 (Core Issue 1581: When are constexpr member
functions defined?).</P>

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