@@ -1341,22 +1341,30 @@ $(GNAME IndexExpression):
1341
1341
$(GLINK PostfixExpression) $(D [) $(GLINK ArgumentList) $(D ])
1342
1342
)
1343
1343
1344
- $(P $(I PostfixExpression) is evaluated. If $(I PostfixExpression) is an
1345
- expression of type static array or dynamic array, the symbol $(DOLLAR) is set to
1346
- be the number of elements in the array. If $(I PostfixExpression) is a $(I
1347
- ValueSeq), the symbol $(DOLLAR) is set to be the number of elements
1348
- in the sequence. A new declaration scope is created for the evaluation of the
1349
- $(GLINK ArgumentList) and $(DOLLAR) appears in that scope only.)
1350
-
1351
- $(P If $(I PostfixExpression) is a $(I ValueSeq),
1352
- then the $(GLINK ArgumentList) must consist of only one argument,
1344
+ $(P $(I PostfixExpression) is evaluated.
1345
+ If $(I PostfixExpression) is an expression of static or
1346
+ dynamic array type, the result of the indexing is an lvalue
1347
+ of the *i*th element in the array, where `i` is an integer
1348
+ evaluated from $(I ArgumentList).
1349
+ If $(I PostfixExpression) is a pointer `p`, the result is
1350
+ `*(p + i)` (see $(RELATIVE_LINK2 pointer_arithmetic, Pointer Arithmetic)).
1351
+ )
1352
+
1353
+ $(P If $(I PostfixExpression) is a $(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq))
1354
+ then the $(I ArgumentList) must consist of only one argument,
1353
1355
and that must be statically evaluatable to an integral constant.
1354
1356
That integral constant $(I n) then selects the $(I n)th
1355
1357
expression in the $(I ValueSeq), which is the result
1356
1358
of the $(I IndexExpression).
1357
1359
It is an error if $(I n) is out of bounds of the $(I ValueSeq).
1358
1360
)
1359
1361
1362
+ $(P The special variable `$` is declared and set to be the number
1363
+ of elements in the $(I PostfixExpression) (when available).
1364
+ A new declaration scope is created for the evaluation of the
1365
+ $(I ArgumentList) and `$` appears in that scope only.
1366
+ )
1367
+
1360
1368
$(H2 $(LNAME2 slice_expressions, Slice Expressions))
1361
1369
1362
1370
$(GRAMMAR
@@ -1372,43 +1380,59 @@ $(GNAME Slice):
1372
1380
)
1373
1381
1374
1382
$(P $(I PostfixExpression) is evaluated.
1375
- if $(I PostfixExpression) is an expression of type
1376
- static array or dynamic array, the special variable $(DOLLAR)
1377
- is declared and set to be the length of the array.
1378
- A new declaration scope is created for the evaluation of the
1379
- $(GLINK AssignExpression)..$(GLINK AssignExpression)
1380
- and $(DOLLAR) appears in that scope only.
1383
+ If $(I PostfixExpression) is a static or dynamic
1384
+ array `a`, the result of the slice is a dynamic array
1385
+ referencing elements `a[i]` to `a[j-1]` inclusive, where `i`
1386
+ and `j` are integers evaluated from the first and second $(I
1387
+ AssignExpression) respectively.
1388
+ )
1389
+
1390
+ $(P If $(I PostfixExpression) is a pointer `p`, the result
1391
+ will be a dynamic array referencing elements from `p[i]` to `p[j-1]`
1392
+ inclusive, where `i` and `j` are integers evaluated from the
1393
+ first and second $(I AssignExpression) respectively.
1394
+ )
1395
+
1396
+ $(P If $(I PostfixExpression) is a $(DDSUBLINK spec/template, variadic-templates, $(I ValueSeq)), then
1397
+ the result of the slice is a new $(I ValueSeq) formed
1398
+ from the upper and lower bounds, which must statically evaluate
1399
+ to integral constants.
1400
+ It is an error if those bounds are out of range.
1381
1401
)
1382
1402
1383
1403
$(P The first $(I AssignExpression) is taken to be the inclusive
1384
1404
lower bound
1385
1405
of the slice, and the second $(I AssignExpression) is the
1386
1406
exclusive upper bound.
1387
- The result of the expression is a slice of the $(I PostfixExpression)
1388
- array.
1407
+ The result of the expression is a slice of the elements in $(I PostfixExpression).
1389
1408
)
1390
1409
1391
- $(P If the $(D [ ]) form is used, the slice is of the entire
1392
- array.
1410
+ $(P The special variable `$` is declared and set to be the number
1411
+ of elements in the $(I PostfixExpression) (when available).
1412
+ A new declaration scope is created for the evaluation of the
1413
+ $(I AssignExpression)`..`$(I AssignExpression) and `$` appears in
1414
+ that scope only.
1393
1415
)
1394
1416
1395
- $(P The type of the slice is a dynamic array of the element
1396
- type of the $(I PostfixExpression).
1417
+ $(P If the $(D [ ]) form is used, the slice is of all the elements in $(I PostfixExpression).
1397
1418
)
1398
1419
1399
1420
$(P A $(I SliceExpression) is not a modifiable lvalue.)
1400
1421
1422
+ $(H3 $(LNAME2 slice_to_static_array, Slice Conversion to Static Array))
1423
+
1401
1424
$(P If the slice bounds can be known at compile time, the slice expression
1402
- is implicitly convertible to an lvalue of static array. For example:)
1425
+ may be implicitly convertible to an lvalue of static array. For example:)
1403
1426
1404
1427
-------------
1405
1428
arr[a .. b] // typed T[]
1406
1429
-------------
1407
1430
1408
- If both $(CODE a) and $(CODE b) are integers (may be constant-folded),
1431
+ If both $(CODE a) and $(CODE b) are integers (which may be constant-folded),
1409
1432
the slice expression can be converted to a static array type
1410
1433
$(D T[b - a]).
1411
1434
1435
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1412
1436
-------------
1413
1437
void foo(int[2] a)
1414
1438
{
@@ -1433,9 +1457,10 @@ $(GNAME Slice):
1433
1457
bar(arr[1 .. 3]);
1434
1458
assert(arr == [1, 4, 5]);
1435
1459
1436
- //baz(arr[1 .. 3]); // cannot match length
1460
+ //baz(arr[1 .. 3]); // cannot match length
1437
1461
}
1438
1462
-------------
1463
+ )
1439
1464
1440
1465
$(P The following forms of slice expression can be convertible to a static array
1441
1466
type:)
@@ -1456,14 +1481,6 @@ type:)
1456
1481
$(TROW $(D arr[e-a .. e-b]), $(D a - b) $(I if) $(D a >= b))
1457
1482
)
1458
1483
1459
- $(P If $(I PostfixExpression) is a $(I ValueSeq), then
1460
- the result of the slice is a new $(I ValueSeq) formed
1461
- from the upper and lower bounds, which must statically evaluate
1462
- to integral constants.
1463
- It is an error if those
1464
- bounds are out of range.
1465
- )
1466
-
1467
1484
$(H2 $(LNAME2 primary_expressions, Primary Expressions))
1468
1485
1469
1486
$(GRAMMAR
0 commit comments