@@ -1281,43 +1281,55 @@ writefln("the string is '%s'", str);
1281
1281
1282
1282
$(H3 $(LNAME2 void_arrays, Void Arrays))
1283
1283
1284
- $(P There is a special type of array which acts as a wildcard that can hold
1285
- arrays of any kind, declared as $(D void[]) . Void arrays are used for
1284
+ $(P There are special types of array with `void` element type which can hold
1285
+ arrays of any kind. Void arrays are used for
1286
1286
low-level operations where some kind of array data is being handled, but
1287
1287
the exact type of the array elements are unimportant. The $(D .length) of a
1288
1288
void array is the length of the data in bytes, rather than the number of
1289
- elements in its original type. Array indices in indexing and slicing
1289
+ elements in its original type. Array indices in slicing
1290
1290
operations are interpreted as byte indices.)
1291
1291
1292
- $(P Arrays of any type can be implicitly converted to a void array; the
1292
+ $(P Arrays of any type can be implicitly converted to a (tail qualified) void array - the
1293
1293
compiler inserts the appropriate calculations so that the $(D .length) of
1294
1294
the resulting array's size is in bytes rather than number of elements. Void
1295
- arrays cannot be converted back to the original type without using a cast,
1295
+ arrays cannot be converted back to the original type without using an
1296
+ $(DDSUBLINK spec/expression, cast_array, array cast),
1296
1297
and it is an error to convert to an array type whose element size does not
1297
1298
evenly divide the length of the void array.)
1298
1299
1299
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1300
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1300
1301
---------
1301
1302
void main()
1302
1303
{
1303
1304
int[] data1 = [1,2,3];
1304
- long[] data2;
1305
1305
1306
1306
void[] arr = data1; // OK, int[] implicit converts to void[].
1307
1307
assert(data1.length == 3);
1308
1308
assert(arr.length == 12); // length is implicitly converted to bytes.
1309
1309
1310
- //data1 = arr; // Illegal: void[] does not implicitly
1310
+ arr[0..4] = [5]; // Assign first 4 bytes to 1 int element
1311
+ assert(data1 == [5,2,3]);
1312
+
1313
+ //data1 = arr; // Error: void[] does not implicitly
1311
1314
// convert to int[].
1312
- int[] data3 = cast(int[]) arr; // OK, can convert with explicit cast.
1313
- data2 = cast(long[]) arr; // Runtime error: long.sizeof == 8, which
1315
+ int[] data2 = cast(int[]) arr; // OK, can convert with explicit cast.
1316
+ assert(data2 is data1);
1317
+ }
1318
+ ---------
1319
+ )
1320
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1321
+ ---------
1322
+ void main()
1323
+ {
1324
+ void[] arr = new void[12];
1325
+ long[] bad = cast(long[]) arr; // Runtime error: long.sizeof == 8, which
1314
1326
// does not divide arr.length, which is 12
1315
1327
// bytes.
1316
1328
}
1317
1329
---------
1318
1330
)
1319
1331
1320
- $(P Void arrays can also be static if their length is known at
1332
+ $(P Void arrays can be static arrays if their length is known at
1321
1333
compile-time. The length is specified in bytes:)
1322
1334
1323
1335
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
0 commit comments