@@ -286,6 +286,33 @@ $(H3 $(LEGACY_LNAME2 Integer Promotions, integer-promotions, Integer Promotions)
286
286
column.
287
287
)
288
288
289
+ $(P Integer promotion applies to each operand of a binary expression:)
290
+
291
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
292
+ ---
293
+ byte a;
294
+ auto b = a + a;
295
+ static assert(is(typeof(b) == int));
296
+ // error: can't implicitly convert expression of type int to byte:
297
+ //byte c = a + a;
298
+
299
+ ushort d;
300
+ // error: can't implicitly convert expression of type int to ushort:
301
+ //d = d * d;
302
+ int e = d * d; // OK
303
+ static assert(is(typeof(int() * d) == int));
304
+
305
+ dchar f;
306
+ static assert(is(typeof(f - f) == uint));
307
+ ---
308
+ )
309
+
310
+ $(RATIONALE
311
+ * 32-bit integer operations are often faster than smaller integer types
312
+ for single variables on modern architectures.
313
+ * Promotion helps avoid accidental overflow which is more common with small integer types.
314
+ )
315
+
289
316
$(H3 $(LEGACY_LNAME2 Usual Arithmetic Conversions, usual-arithmetic-conversions, Usual Arithmetic Conversions))
290
317
291
318
$(P The usual arithmetic conversions convert operands of binary
@@ -322,21 +349,30 @@ $(H3 $(LEGACY_LNAME2 Usual Arithmetic Conversions, usual-arithmetic-conversions,
322
349
)
323
350
)
324
351
352
+ $(RATIONALE The above rules follow C99, which makes porting code from C easier.)
353
+
354
+ $(P $(B Example:) Signed and unsigned conversions:)
325
355
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
326
356
---
327
- byte a;
328
- int b = a * a;
329
- auto c = a + a;
330
- static assert(is(typeof(c) == int));
357
+ int i;
358
+ uint u;
359
+ static assert(is(typeof(i + u) == uint));
360
+ static assert(is(typeof(short() + u) == uint));
361
+ static assert(is(typeof(ulong() + i) == ulong));
362
+ static assert(is(typeof(long() - u) == long));
363
+ static assert(is(typeof(long() * ulong()) == ulong));
364
+ ---
365
+ )
331
366
332
- ushort d;
333
- static assert(is(typeof(b * d) == int));
367
+ $(P $(B Example:) Floating point:)
368
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
369
+ ---
370
+ float f;
371
+ static assert(is(typeof(f + ulong()) == float));
334
372
335
- uint e;
336
- static assert(is(typeof(b + e) == uint));
337
- static assert(is(typeof(d + e) == uint));
338
- static assert(is(typeof(ulong() + b) == ulong));
339
- static assert(is(typeof(long() - e) == long));
373
+ double d;
374
+ static assert(is(typeof(f * d) == double));
375
+ static assert(is(typeof(real() / d) == real));
340
376
---
341
377
)
342
378
0 commit comments