Skip to content

Commit 010f14c

Browse files
author
Stefanus Du Toit
committed
N3639 Runtime-sized arrays with automatic storage duration
N3639 [dcl.array] paragraph 1 In [dcl.array]/1, removed bullet points from the last part of the major bits of added text. The bulleted list was preceded by nothing and appeared out of nowhere. Added an index entry for "array of runtime bound" and "runtime bound, array of". In [support.dynamic], bad_array_length also added to <new> synopsis. Typeset types in comments to [dcl.array]/1 as code, including two existing instances not added by the paper. Improved grammar as follows: "is invoked ... after the lifetime of the array ended" -> "... has ended" Rearranged commas in [dcl.fct]/8 for consistent and correct grammar.
1 parent 89281b5 commit 010f14c

10 files changed

Lines changed: 150 additions & 68 deletions

source/basic.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3551,7 +3551,7 @@
35513551
\begin{itemize}
35523552
\item a scalar type; or
35533553
\item a reference type; or
3554-
\item an array of literal type; or
3554+
\item an array of literal type other than an array of runtime bound; or
35553555
\item a class type (Clause~\ref{class}) that
35563556
has all of the following properties:
35573557
\begin{itemize}

source/classes.tex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,11 @@
534534

535535
\pnum
536536
\indextext{class~object!member}%
537-
Non-\tcode{static}~(\ref{class.static}) data members shall not have
538-
incomplete types. In particular, a class \tcode{C} shall not contain a
539-
non-static member of class \tcode{C}, but it can contain a pointer or
540-
reference to an object of class \tcode{C}.
537+
A non-\tcode{static}~(\ref{class.static}) data member shall not have
538+
incomplete type or type ``array of runtime bound''. \enternote In particular,
539+
a class \tcode{C} shall not contain a non-static member of class \tcode{C}, but
540+
it can contain a pointer or reference to an object of class \tcode{C}.
541+
\exitnote
541542

542543
\pnum
543544
\enternote

source/conversions.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@
175175
\pnum
176176
\indextext{conversion!array-to-pointer}%
177177
\indextext{decay|see{conversion, array to pointer; conversion, function to pointer}}%
178-
An lvalue or rvalue of type ``array of \tcode{N} \tcode{T}'' or ``array
179-
of unknown bound of \tcode{T}'' can be converted to a prvalue of type
180-
``pointer to \tcode{T}''. The result is a pointer to the first element
181-
of the array.
178+
An expression of type ``array of \tcode{N} \tcode{T}'', ``array of runtime
179+
bound of \tcode{T}'', or ``array of unknown bound of \tcode{T}'' can be
180+
converted to a prvalue of type ``pointer to \tcode{T}''. The result is a
181+
pointer to the first element of the array.
182182

183183
\rSec1[conv.func]{Function-to-pointer conversion}
184184

source/declarations.tex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@
611611

612612
\exitexample
613613

614+
\pnum
615+
A \grammarterm{typedef-name} shall not name an array of runtime bound.
616+
614617
\pnum
615618
\indextext{redefinition!\idxcode{typedef}}%
616619
In a given non-class scope, a \tcode{typedef} specifier can be used to
@@ -1377,9 +1380,11 @@
13771380
\indextext{type~specifier!\idxcode{decltype}}%
13781381
The type denoted by \tcode{decltype(e)} is defined as follows:
13791382
\begin{itemize}
1380-
\item if \tcode{e} is an unparenthesized \grammarterm{id-expression} or
1381-
an unparenthesized
1382-
class
1383+
\item if \tcode{e} has type ``array of runtime bound'', the program is
1384+
ill-formed
1385+
1386+
\item otherwise, if \tcode{e} is an unparenthesized
1387+
\grammarterm{id-expression} or an unparenthesized class
13831388
member access~(\ref{expr.ref}), \tcode{decltype(e)} is the
13841389
type of the entity named by \tcode{e}. If there is no such entity, or
13851390
if \tcode{e} names a set of overloaded functions, the program is

source/declarators.tex

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
\nontermdef{noptr-declarator}\br
121121
declarator-id attribute-specifier-seq\opt\br
122122
noptr-declarator parameters-and-qualifiers\br
123-
noptr-declarator \terminal{[} constant-expression\opt \terminal{]} attribute-specifier-seq\opt\br
123+
noptr-declarator \terminal{[} expression\opt \terminal{]} attribute-specifier-seq\opt\br
124124
\terminal{(} ptr-declarator \terminal{)}
125125
\end{bnf}
126126

@@ -599,7 +599,10 @@
599599
The
600600
\grammarterm{cv-qualifier}{s}
601601
apply to the pointer and not to the object pointed to.
602-
Similarly, the optional \grammarterm{attribute-specifier-seq}~(\ref{dcl.attr.grammar}) appertains to the pointer and not to the object pointed to.
602+
Similarly, the optional
603+
\grammarterm{attribute-specifier-seq}~(\ref{dcl.attr.grammar}) appertains to
604+
the pointer and not to the object pointed to. There shall be no pointers to
605+
arrays of runtime bound.
603606

604607
\pnum
605608
\enterexample
@@ -821,8 +824,8 @@
821824

822825
\pnum
823826
\indextext{restriction!reference}%
824-
There shall be no references to references,
825-
no arrays of references, and no pointers to references.
827+
There shall be no references to references, no references to arrays of runtime
828+
bound, no arrays of references, and no pointers to references.
826829
\indextext{initialization!reference}%
827830
The declaration of a reference shall contain an
828831
\grammarterm{initializer}
@@ -993,7 +996,7 @@
993996
has the form
994997

995998
\begin{ncsimplebnf}
996-
\terminal{D1 [} constant-expression\opt \terminal{]} attribute-specifier-seq\opt
999+
\terminal{D1 [} expression\opt \terminal{]} attribute-specifier-seq\opt
9971000
\end{ncsimplebnf}
9981001

9991002
and the type of the identifier in the declaration
@@ -1012,41 +1015,57 @@
10121015
\term{element type};
10131016
this type shall not be a reference type, the (possibly cv-qualified) type
10141017
\tcode{void},
1015-
a function type or an abstract class type.
1018+
a function type, an array of unknown or runtime bound, or an abstract class type.
1019+
Except as noted below, if the \grammarterm{expression} is omitted, the type of
1020+
the identifier of \tcode{D} is ``\grammarterm{derived-declarator-type-list}
1021+
array of unknown bound of \tcode{T}''. If the \grammarterm{expression} is
1022+
present, it is implicitly converted to \tcode{std::size_t}. The
1023+
\grammarterm{expression} is erroneous if:
1024+
1025+
\begin{itemize}
1026+
\item
1027+
its value before converting to \tcode{std::size_t} or, in the case of an
1028+
expression of class type, before application of the second standard
1029+
conversion~(\ref{over.ics.user}) is less than or equal to zero;
1030+
1031+
\item
1032+
its value is such that the size of the allocated object would exceed the
1033+
implementation-defined limit (Annex~\ref{implimits});
1034+
1035+
\item
1036+
the initializer of the object is a \grammarterm{braced-init-list} whose number
1037+
of (top-level) \grammarterm{initializer-clause}{s} exceeds the number of
1038+
elements to initialize; or
1039+
1040+
\item
1041+
the object is initialized with a string literal and there are more initializers
1042+
than there are array elements.
1043+
\end{itemize}
1044+
1045+
If the \grammarterm{expression}, after converting to \tcode{std::size_t}, is a
1046+
core constant expression and the \grammarterm{expression} is erroneous, the
1047+
program is ill-formed.
1048+
1049+
If the \grammarterm{expression}, after converting to \tcode{std::size_t}, is a
1050+
core constant expression whose value is \tcode{N}, the type of the identifier
1051+
of \tcode{D} is ``\grammarterm{derived-declarator-type-list} array of
1052+
\tcode{N T}''.
1053+
1054+
\indextext{array of runtime bound}%
1055+
\indextext{runtime bound!array of|see{array of runtime bound}}%
1056+
Otherwise, the type of the identifier of \tcode{D} is
1057+
``\grammarterm{derived-declarator-type-list} array of runtime bound of
1058+
\tcode{T}'' and the value of the \grammarterm{expression} designates the number
1059+
of elements \tcode{N} in the array. If the \grammarterm{expression} is
1060+
erroneous, an exception of a type that would match a
1061+
handler~(\ref{except.handle}) of type
1062+
\tcode{std::bad_array_length}~(\ref{bad.array.length}) is thrown.
1063+
10161064
\indextext{declaration!array}%
1017-
If the
1018-
\grammarterm{constant-expression}
1019-
(\ref{expr.const}) is present, it shall be a converted constant
1020-
expression of type \tcode{std\colcol{}size_t} and
1021-
its value shall be greater than zero.
1022-
The constant expression specifies the
1023-
\indextext{array!bound}%
1024-
\indextext{bound,~of~array}%
1025-
\term{bound}
1026-
of (number of elements in) the array.
1027-
If the value of the constant expression is
1028-
\tcode{N},
1029-
the array has
1030-
\tcode{N}
1031-
elements numbered
1032-
\tcode{0}
1033-
to
1034-
\tcode{N-1},
1035-
and the type of the identifier of
1036-
\tcode{D}
1037-
is ``\nonterminal{derived-declarator-type-list} array of
1038-
\tcode{N}
1039-
\tcode{T}''.
10401065
An object of array type contains a contiguously allocated non-empty set of
10411066
\tcode{N}
10421067
subobjects of type
10431068
\tcode{T}.
1044-
Except as noted below, if
1045-
the constant expression is omitted, the type of the identifier of
1046-
\tcode{D}
1047-
is ``\nonterminal{derived-declarator-type-list} array of unknown bound of
1048-
\tcode{T}'',
1049-
an incomplete object type.
10501069
The type ``\nonterminal{derived-declarator-type-list} array of
10511070
\tcode{N}
10521071
\tcode{T}''
@@ -1065,14 +1084,19 @@
10651084
\tcode{T}'',
10661085
and similarly for
10671086
``array of unknown bound of
1068-
\tcode{T}''.
1087+
\tcode{T}'' and ``array of runtime bound of \tcode{T}''.
10691088
The optional \grammarterm{attribute-specifier-seq} appertains to the array.
10701089
\enterexample
10711090

10721091
\begin{codeblock}
10731092
typedef int A[5], AA[2][3];
1074-
typedef const A CA; // type is ``array of 5 const int''
1075-
typedef const AA CAA; // type is ``array of 2 array of 3 const int''
1093+
typedef const A CA; // type is ``array of 5 \tcode{const int}''
1094+
typedef const AA CAA; // type is ``array of 2 array of 3 \tcode{const int}''
1095+
1096+
void f(unsigned int n) {
1097+
int a[n]; // type of \tcode{a} is ``array of runtime bound of \tcode{int}''
1098+
}
1099+
10761100
\end{codeblock}
10771101
\exitexample
10781102
\enternote
@@ -1095,10 +1119,7 @@
10951119

10961120
\pnum
10971121
When several ``array of'' specifications are adjacent, a multidimensional
1098-
array is created;
1099-
only the first of
1100-
the constant expressions that specify the bounds
1101-
of the arrays may be omitted.
1122+
array is created.
11021123
In addition to declarations in which an incomplete object type is allowed,
11031124
an array bound may be omitted in some cases in the declaration of a function
11041125
parameter~(\ref{dcl.fct}).
@@ -1122,6 +1143,20 @@
11221143
be the same as in that earlier declaration, and similarly for the definition
11231144
of a static data member of a class.
11241145

1146+
\pnum
1147+
An array of runtime bound shall only be used as the type of a local object with
1148+
automatic storage duration. If the size of the array exceeds the size of the
1149+
memory available for objects with automatic storage duration, the behavior is
1150+
undefined.\footnote{Implementations that detect this case are encouraged to
1151+
throw an exception that would match a handler~(\ref{except.handle}) of type
1152+
\tcode{std::bad_array_length}~(\ref{bad.array.length}).}
1153+
It is unspecified whether a global allocation
1154+
function~(\ref{basic.stc.dynamic}) is invoked to obtain storage for the array.
1155+
If it is invoked, the corresponding global deallocation function is invoked to
1156+
release the storage after the lifetime of the array has
1157+
ended.\footnote{Alternatively, an implementation could allocate such an array
1158+
on the usual stack or obtain storage via \tcode{malloc}~(\ref{c.malloc}).}
1159+
11251160
\pnum
11261161
\enterexample
11271162
\indextext{example!subscripting}%
@@ -1536,8 +1571,9 @@
15361571

15371572
\pnum
15381573
If the type of a parameter includes a type of the form
1539-
``pointer to array of unknown bound of \tcode{T}'' or
1540-
``reference to array of unknown bound of \tcode{T},''
1574+
``array of runtime bound of \tcode{T}'',
1575+
``pointer to array of unknown bound of \tcode{T}'', or
1576+
``reference to array of unknown bound of \tcode{T}'',
15411577
the program is ill-formed.\footnote{This excludes parameters of type
15421578
``\nonterminal{ptr-arr-seq} \tcode{T2}'' where \tcode{T2} is
15431579
``pointer to array of unknown bound of \tcode{T}'' and where
@@ -2871,7 +2907,7 @@
28712907
\exitexample
28722908

28732909
\pnum
2874-
An
2910+
For types other than arrays of runtime bound~(\ref{dcl.array}), an
28752911
\grammarterm{initializer-list}
28762912
is ill-formed if the number of
28772913
\grammarterm{initializer-clause}{s}
@@ -3185,7 +3221,8 @@
31853221
\exitexample
31863222

31873223
\pnum
3188-
There shall not be more initializers than there are array elements.
3224+
\enternote There cannot be more initializers than there are array elements;
3225+
see~\ref{dcl.array}.
31893226
\enterexample
31903227

31913228
\begin{codeblock}
@@ -3194,7 +3231,7 @@
31943231

31953232
is ill-formed since there is no space for the implied trailing
31963233
\tcode{'\textbackslash 0'}.
3197-
\exitexample
3234+
\exitexample \exitnote
31983235

31993236
\pnum
32003237
If there are fewer initializers than there are array elements, each element not

source/exceptions.tex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@
202202
functions~(\ref{basic.stc.dynamic.allocation}),
203203
\tcode{dynamic_cast}~(\ref{expr.dynamic.cast}),
204204
\tcode{typeid}~(\ref{expr.typeid}),
205-
\grammarterm{new-expression}~(\ref{expr.new}), and standard library
205+
\grammarterm{new-expression}~(\ref{expr.new}), array of runtime
206+
bound~(\ref{dcl.array}), and standard library
206207
functions~(\ref{structure.specifications}).
207208
\exitnote
208209
An object is passed and the type of that object determines which handlers

source/expressions.tex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,15 @@
842842
corresponding captured entity if the entity is not a reference to an object, or the
843843
referenced type otherwise. \enternote If the captured entity is a reference to a
844844
function, the corresponding data member is also a reference to a function. \exitnote
845+
An array of runtime bound~(\ref{dcl.array}) shall not be captured by copy.
845846

846847
\pnum
847848
An entity is \indexdefn{captured!by reference}\term{captured by reference} if it is implicitly or explicitly
848849
captured but not captured by copy. It is unspecified whether additional unnamed
849850
non-static data members are declared in the closure type for entities captured by
850-
reference.
851+
reference. \enternote Capturing by reference an array of runtime bound also
852+
implicitly captures the value of the bound to support the range-based for
853+
statement~(\ref{stmt.ranged}). \exitnote
851854

852855
\pnum
853856
If a \grammarterm{lambda-expression} \tcode{m2} captures an entity and that entity is
@@ -1674,6 +1677,9 @@
16741677
of the program. Whether or not the destructor is called for the
16751678
\tcode{std::type_info} object at the end of the program is unspecified.
16761679

1680+
\pnum
1681+
The \tcode{typeid} operator shall not be applied to an array of runtime bound.
1682+
16771683
\pnum
16781684
When \tcode{typeid} is applied to a glvalue expression whose type is a
16791685
polymorphic class type~(\ref{class.virtual}), the result refers to a
@@ -2335,7 +2341,8 @@
23352341
\indextext{name!address~of cv-qualified}%
23362342
\indextext{expression!pointer~to~member constant}%
23372343
The result of the unary \tcode{\&} operator is a pointer to its operand.
2338-
The operand shall be an lvalue or a \grammarterm{qualified-id}.
2344+
The operand shall be either an lvalue of type other than ``array of runtime bound''
2345+
or a \grammarterm{qualified-id}.
23392346
If the operand is a \grammarterm{qualified-id} naming a non-static member \tcode{m}
23402347
of some class \tcode{C} with type \tcode{T}, the result has type ``pointer to member
23412348
of class \tcode{C} of type \tcode{T}'' and is a prvalue designating \tcode{C::m}.
@@ -2475,8 +2482,9 @@
24752482
\grammarterm{type-id}.
24762483
\indextext{type!incomplete}%
24772484
The \tcode{sizeof} operator shall not be applied to an expression that
2478-
has function or incomplete type, to an enumeration type whose underlying type is not fixed before all
2479-
its enumerators have been declared, to the parenthesized name of such
2485+
has function or incomplete type, to an enumeration type whose underlying type
2486+
is not fixed before all its enumerators have been declared, to an array of
2487+
runtime bound, to the parenthesized name of such
24802488
types, or to a glvalue that designates a bit-field.
24812489
\tcode{sizeof(char)}, \tcode{sizeof(signed char)} and
24822490
\tcode{sizeof(unsigned char)} are \tcode{1}. The result of

source/grammar.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@
12471247
\nontermdef{noptr-declarator}\br
12481248
declarator-id attribute-specifier-seq\opt\br
12491249
noptr-declarator parameters-and-qualifiers\br
1250-
noptr-declarator \terminal{[} constant-expression\opt \terminal{]} attribute-specifier-seq\opt\br
1250+
noptr-declarator \terminal{[} expression\opt \terminal{]} attribute-specifier-seq\opt\br
12511251
\terminal{(} ptr-declarator \terminal{)}
12521252
\end{bnf}
12531253

0 commit comments

Comments
 (0)