Common type of ubyte and const ubyte is int

Steven Schveighoffer schveiguy at gmail.com
Wed May 1 14:42:25 UTC 2024


On Tuesday, 30 April 2024 at 21:18:36 UTC, Timon Gehr wrote:
> On 4/30/24 20:19, Steven Schveighoffer wrote:
>> ```d
>> ubyte a;
>> const(ubyte) b;
>> 
>> auto x = true ? a : b;
>> 
>> pragma(msg, typeof(x)); // int
>> ```
>> 
>> Why? This isn't a VRP problem, as both are `ubyte`.
>> 
>> Related issue
>> 
>> https://issues.dlang.org/show_bug.cgi?id=19817
>> 
>> -Steve
>
> FWIW, my frontend says `ubyte` here and there is a comment 
> somewhere that claims it implements some rules from TDPL page 
> 60.

Thanks to phone OCR, this is what is on that page:

```
1. If a and b have the same type, T is that type;
2. else if a and b are integrals, first promote anything smaller 
than 32-bit to int, then choose T as the larger type, with a 
preference for unsigned type if tied in size;
3. else if one is an integral and the other is a floating-point 
type, T is the floating-point type;
4. else if both have floating-point types, T is the larger of the 
two;
5. else if the types have a common supertype (e.g., base class), 
T is that supertype (we will return to this topic in Chapter 6);
6. else try implicitly converting a to b's type and b to a's 
type; if exactly one of these succeeds, T is the type of the 
successful conversion target;
7. else the expression is in error.
```

It seems rule 2 would apply instead of rule 6? but I don't like 
it.

-Steve


More information about the Digitalmars-d mailing list