Skip to content

Commit 670db0b

Browse files
Dawn Perchikzygoloid
authored andcommitted
CWG1704 Type checking in explicit instantiation of variable templates
Also fixes CWG1728.
1 parent b06c3ee commit 670db0b

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

source/templates.tex

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4889,13 +4889,18 @@
48894889

48904890
\pnum
48914891
If the explicit instantiation is for a class or member class, the
4892-
\grammarterm{elaborated-type-specifier} in the \grammarterm{declaration} shall
4893-
include a \grammarterm{simple-template-id}. If the explicit instantiation is for
4892+
\grammarterm{elaborated-type-specifier} in the \grammarterm{declaration}
4893+
shall include a \grammarterm{simple-template-id};
4894+
otherwise, the \grammarterm{declaration}
4895+
shall be a \grammarterm{simple-declaration} whose \grammarterm{init-declarator-list}
4896+
comprises a single \grammarterm{init-declarator}
4897+
that does not have an \grammarterm{initializer}.
4898+
If the explicit instantiation is for
48944899
a function or member function,
48954900
the
48964901
\grammarterm{unqualified-id}
48974902
in the
4898-
\grammarterm{declaration}
4903+
\grammarterm{declarator}
48994904
shall be either a
49004905
\grammarterm{template-id}
49014906
or, where all template arguments can be deduced, a
@@ -4915,9 +4920,9 @@
49154920
the name of the class template specialization in the
49164921
\grammarterm{qualified-id}
49174922
for the member name shall be a \grammarterm{simple-template-id}.
4918-
If the explicit instantiation is for a variable, the
4919-
\grammarterm{unqualified-id} in the declaration shall be a
4920-
\grammarterm{template-id}.
4923+
If the explicit instantiation is for a variable template specialization,
4924+
the \grammarterm{unqualified-id} in the \grammarterm{declarator}
4925+
shall be a \grammarterm{simple-template-id}.
49214926
An explicit instantiation shall appear in an enclosing namespace
49224927
of its template. If the name declared in the explicit
49234928
instantiation is an unqualified name, the explicit instantiation
@@ -4958,6 +4963,28 @@
49584963
of the explicit instantiation names an implicitly-declared special member
49594964
function (Clause~\ref{special}), the program is ill-formed.
49604965

4966+
\pnum
4967+
The \grammarterm{declaration} in an \grammarterm{explicit-instantiation} and the \grammarterm{declaration} produced by the corresponding substitution into the templated function, variable, or class are two declarations of the same entity.
4968+
\begin{note}
4969+
These declarations are required to have matching types as specified in~\ref{basic.link}, except as specified in~\ref{except.spec}.
4970+
\begin{example}
4971+
\begin{codeblock}
4972+
template<typename T> T var = {};
4973+
template float var<float>; // OK, instantiated variable has type \tcode{float}
4974+
template int var<int[16]>[]; // OK, absence of major array bound is permitted
4975+
template int *var<int>; // error: instantiated variable has type \tcode{int}
4976+
4977+
template<typename T> auto av = T();
4978+
template int av<int>; // OK, variable with type \tcode{int} can be redeclared with type \tcode{auto}
4979+
4980+
template<typename T> auto f() {}
4981+
template void f<int>(); // error: function with deduced return type
4982+
// redeclared with non-deduced return type~(\ref{dcl.spec.auto})
4983+
\end{codeblock}
4984+
\end{example}
4985+
\end{note}
4986+
Despite its syntactic form, the \grammarterm{declaration} in an \grammarterm{explicit-instantiation} for a variable is not itself a definition and does not conflict with the definition instantiated by an explicit instantiation definition for that variable.
4987+
49614988
\pnum
49624989
For a given set of template arguments, if an explicit
49634990
instantiation of a template appears after a declaration of
@@ -5033,8 +5060,9 @@
50335060
literal types,
50345061
variables of reference types, and class template specializations,
50355062
explicit instantiation declarations have the
5036-
effect of suppressing the implicit instantiation of the entity to which they
5037-
refer. \begin{note} The intent is that an inline function that is the
5063+
effect of suppressing the implicit instantiation
5064+
of the definition of the entity to which they refer.
5065+
\begin{note} The intent is that an inline function that is the
50385066
subject of an explicit instantiation declaration will still be implicitly
50395067
instantiated when odr-used~(\ref{basic.def.odr}) so that the body can be considered for inlining, but
50405068
that no out-of-line copy of the inline function would be generated in the

0 commit comments

Comments
 (0)