Skip to content

Commit 16e5b37

Browse files
eisenwavetkoeppe
authored andcommitted
P4149R1 Define "immediate context"
Fixes NB US 54-100 (C++26 CD).
1 parent c1b2b44 commit 16e5b37

File tree

2 files changed

+80
-64
lines changed

2 files changed

+80
-64
lines changed

source/exceptions.tex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,9 @@
996996
declaration (e.g., an explicit specialization or an overriding virtual
997997
function);
998998

999-
\item the function is defined; or
999+
\item the function is defined;
1000+
1001+
\item the function is represented by a reflection; or
10001002

10011003
\item the exception specification is needed for a defaulted
10021004
function that calls the function.
@@ -1009,10 +1011,7 @@
10091011
\end{note}
10101012
\end{itemize}
10111013
The exception specification of a defaulted
1012-
function is evaluated as described above only when needed; similarly, the
1013-
\grammarterm{noexcept-specifier} of a specialization
1014-
of a templated function
1015-
is instantiated only when needed.
1014+
function is evaluated as described above only when needed.
10161015
%
10171016
\indextext{exception specification|)}
10181017

source/templates.tex

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,31 +2680,30 @@
26802680
which shall be a class or variable template.
26812681

26822682
\pnum
2683+
A \defnadj{separately instantiated}{construct}
2684+
of a templated function \tcode{F} is a
2685+
\begin{itemize}
2686+
\item default argument,
2687+
\item \grammarterm{noexcept-specifier}, or
2688+
\item \grammarterm{function-contract-specifier}
2689+
\end{itemize}
2690+
of \tcode{F}.
26832691
For purposes of name lookup and instantiation,
2684-
default arguments,
2685-
\grammarterm{type-constraint}{s},
2686-
\grammarterm{requires-clause}{s}\iref{temp.pre},
2687-
and
2688-
\grammarterm{noexcept-specifier}{s}
2689-
of function templates
2690-
and
2691-
of member functions of class templates
2692+
separately instantiated constructs,
2693+
\grammarterm{type-constraint}{s}, and
2694+
\grammarterm{requires-clause}{s}\iref{temp.pre}
26922695
are considered definitions;
26932696
each
2694-
default argument,
2695-
\grammarterm{type-constraint},
2696-
\grammarterm{requires-clause},
2697-
or
2698-
\grammarterm{noexcept-specifier}
2697+
separately instantiated construct,
2698+
\grammarterm{type-constraint}, or
2699+
\grammarterm{requires-clause}
26992700
is a separate definition
27002701
which is unrelated
27012702
to the templated function definition or
27022703
to any other
2703-
default arguments,
2704-
\grammarterm{type-constraint}{s},
2705-
\grammarterm{requires-clause}{s},
2706-
or
2707-
\grammarterm{noexcept-specifier}{s}.
2704+
separately instantiated constructs,
2705+
\grammarterm{type-constraint}{s}, or
2706+
\grammarterm{requires-clause}{s}.
27082707
For the purpose of instantiation, the substatements of a constexpr if
27092708
statement\iref{stmt.if} are considered definitions.
27102709
For the purpose of name lookup and instantiation,
@@ -6649,19 +6648,6 @@
66496648
X<char> ch; // error: incomplete type \tcode{X<char>}
66506649
\end{codeblock}
66516650
\end{example}
6652-
\begin{note}
6653-
Within a template declaration,
6654-
a local class\iref{class.local} or enumeration and the members of
6655-
a local class are never considered to be entities that can be separately
6656-
instantiated (this includes their default arguments,
6657-
\grammarterm{noexcept-specifier}{s}, and non-static data member
6658-
initializers, if any,
6659-
but not their \grammarterm{type-constraint}{s} or \grammarterm{requires-clause}{s}).
6660-
As a result, the dependent names are looked up, the
6661-
semantic constraints are checked, and any templates used are instantiated as
6662-
part of the instantiation of the entity within which the local class or
6663-
enumeration is declared.
6664-
\end{note}
66656651

66666652
\pnum
66676653
The implicit instantiation of a class template specialization causes
@@ -6681,10 +6667,6 @@
66816667
unscoped member enumerations, and
66826668
member anonymous unions.
66836669
\end{itemize}
6684-
The implicit instantiation of a class template specialization
6685-
does not cause the implicit instantiation of
6686-
default arguments or \grammarterm{noexcept-specifier}{s}
6687-
of the class member functions.
66886670
\begin{example}
66896671
\begin{codeblock}
66906672
template<class T>
@@ -6748,11 +6730,6 @@
67486730
implicitly instantiated when it is referenced in a context that
67496731
requires a function definition to exist or
67506732
if the existence of the definition affects the semantics of the program.
6751-
Unless a call is to a function template explicit specialization or
6752-
to a member function of an explicitly specialized class template,
6753-
a default argument for a function template or a member function of a
6754-
class template is implicitly instantiated when the function is
6755-
called in a context that requires the value of the default argument.
67566733
\begin{note}
67576734
An inline function
67586735
that is the subject of an explicit instantiation declaration
@@ -6883,7 +6860,25 @@
68836860
causes specializations in the default member initializer to be instantiated.
68846861

68856862
\pnum
6886-
If a templated function
6863+
The separately instantiated constructs\iref{temp.decls.general}
6864+
of a templated function \tcode{F} that is either
6865+
\begin{itemize}
6866+
\item
6867+
a member of a local class or
6868+
\item
6869+
the function call operator of the closure type
6870+
of a non-generic lambda-expression
6871+
\end{itemize}
6872+
are instantiated when the declaration of \tcode{F} is instantiated.
6873+
\begin{note}
6874+
For the purposes of instantiation,
6875+
these constructs are still considered separately
6876+
from the function to which they belong.
6877+
\end{note}
6878+
6879+
\pnum
6880+
Other than as specified above,
6881+
when a templated function
68876882
\tcode{f}
68886883
is called in a way that requires a default argument to be used,
68896884
the dependent names are looked up, the semantics constraints are checked,
@@ -6902,7 +6897,9 @@
69026897
\tcode{f}.
69036898

69046899
\pnum
6900+
\begin{note}
69056901
Each default argument is instantiated independently.
6902+
\end{note}
69066903
\begin{example}
69076904
\begin{codeblock}
69086905
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
@@ -6920,16 +6917,16 @@
69206917
\end{example}
69216918

69226919
\pnum
6923-
The \grammarterm{noexcept-specifier} and \grammarterm{function-contract-specifier}s
6924-
of a function template specialization
6925-
are not instantiated along with the function declaration;
6926-
they are instantiated
6927-
when needed\iref{except.spec,dcl.contract.func}. If such a
6928-
specifier is needed but has not yet been
6929-
instantiated, the dependent names are looked up, the semantics constraints are
6930-
checked, and the instantiation of any template used in the
6931-
specifier is done as if it were being done as part
6932-
of instantiating the declaration of the specialization at that point.
6920+
Other than as specified above,
6921+
the separately instantiated constructs
6922+
of a specialization of a templated function
6923+
are instantiated only when needed\iref{except.spec,dcl.contract.func}.
6924+
When such a construct is instantiated,
6925+
the dependent names are looked up,
6926+
the semantics constraints are checked, and
6927+
the instantiation of any template used in the construct
6928+
is done as if it were being done as part of instantiating the declaration
6929+
of the specialization at that point.
69336930

69346931
\pnum
69356932
\begin{note}
@@ -8053,20 +8050,40 @@
80538050
\end{note}
80548051
Invalid types and expressions can result in a deduction failure
80558052
only in the immediate context of the deduction substitution loci.
8053+
The \defnadj{immediate}{context} of a deduction substitution locus
8054+
is that deduction substitution locus,
8055+
excluding bodies of \grammarterm{lambda-expression}s and separately instantiated constructs.
8056+
\begin{example}
8057+
\begin{codeblock}
8058+
template<typename T>
8059+
T* fun(T&& v); // \#1: the deduction substitution locus is the function type
8060+
// ``function of (rvalue reference to \tcode{T}) returning pointer to \tcode{T}''
8061+
8062+
void fun(...); // \#2
8063+
8064+
void test()
8065+
{
8066+
int i;
8067+
fun(i); // selects \#2 (forming the type ``pointer to reference to \tcode{int}'' fails in \#1)
8068+
}
8069+
\end{codeblock}
8070+
\end{example}
8071+
\begin{note}
8072+
Separately instantiated constructs are excluded from the immediate contexts
8073+
even if they are instantiated at the same time.
8074+
\end{note}
80568075
\begin{note}
80578076
The substitution into types and expressions can result
8058-
in effects such as the instantiation of class template specializations and/or
8059-
function template specializations, the generation of implicitly-defined functions,
8060-
etc. Such effects are not in the ``immediate context'' and can result in the
8061-
program being ill-formed.
8077+
in effects such as the instantiation of template specializations,
8078+
the generation of implicitly-defined functions, etc.
8079+
Such effects are not in the immediate context
8080+
and can result in the program being ill-formed.
80628081
\end{note}
80638082

80648083
\pnum
8065-
When substituting into a \grammarterm{lambda-expression},
8066-
substitution into its body is not in the immediate context.
80678084
\begin{note}
8068-
The intent is to avoid requiring implementations to deal with
8069-
substitution failure involving arbitrary statements.
8085+
No \grammarterm{statement} is ever in the immediate context
8086+
of a deduction substitution locus.
80708087
\begin{example}
80718088
\begin{codeblock}
80728089
template <class T>

0 commit comments

Comments
 (0)