Skip to content

Commit 751bbc7

Browse files
authored
Merge pull request #3381 from ntrel/num-conv
[spec/type] Improve numeric type conversion examples Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents 2f83582 + 891bb18 commit 751bbc7

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

spec/expression.dd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,8 @@ $(H3 $(LNAME2 uniform_construction_syntax, Uniform construction syntax for built
21452145
auto b = wchar(); // same as: wchar.init
21462146
---
21472147

2148+
$(P See also: $(DDSUBLINK spec/type, usual-arithmetic-conversions, Usual Arithmetic Conversions).)
2149+
21482150

21492151
$(H3 $(LNAME2 assert_expressions, Assert Expressions))
21502152

spec/type.dd

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,33 @@ $(H3 $(LEGACY_LNAME2 Integer Promotions, integer-promotions, Integer Promotions)
286286
column.
287287
)
288288

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+
289316
$(H3 $(LEGACY_LNAME2 Usual Arithmetic Conversions, usual-arithmetic-conversions, Usual Arithmetic Conversions))
290317

291318
$(P The usual arithmetic conversions convert operands of binary
@@ -322,21 +349,30 @@ $(H3 $(LEGACY_LNAME2 Usual Arithmetic Conversions, usual-arithmetic-conversions,
322349
)
323350
)
324351

352+
$(RATIONALE The above rules follow C99, which makes porting code from C easier.)
353+
354+
$(P $(B Example:) Signed and unsigned conversions:)
325355
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
326356
---
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+
)
331366

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));
334372

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));
340376
---
341377
)
342378

0 commit comments

Comments
 (0)