Annoyance with new integer promotion deprecations

Walter Bright newshound2 at digitalmars.com
Mon Feb 5 21:56:33 UTC 2018


On 2/5/2018 12:45 PM, H. S. Teoh wrote:
> Sticking to C promotion rules is one of the scourges that continue to
> plague D;

It's necessary. Working C expressions cannot be converted to D while introducing 
subtle changes in behavior.

> another example is char -> byte confusion no thanks to C
> traditions:
> 
> 	int f(dchar ch) { return 1; }
> 	int f(byte i) { return 2; }
> 	void main() {
> 		pragma(msg, f('a'));
> 		pragma(msg, f(1));
> 	}
> 
> Exercise for reader: guess compiler output.

'a' and 1 do not match dchar or byte exactly, and require implicit conversions. 
D doesn't have the C++ notion of "better" implicit conversions for function 
arguments, instead it uses the "leastAsSpecialized" C++ notion used for template 
matching, which is better.

The idea is a byte can be implicitly converted to a dchar, but not the other way 
around. Hence, f(byte) is selected as being the "most specialized" match.


More information about the Digitalmars-d mailing list