Value based overload resolution?

Adam D. Ruppe destructionator at gmail.com
Mon Nov 9 22:21:05 UTC 2020


On Monday, 9 November 2020 at 22:04:55 UTC, kdevel wrote:
> It appears to me that the overload resolution may depend on the 
> /value/ of the function argument. According to [1] the type of 
> 1 is int and that of 1L is long.

That's not exactly true because of value range propagation. It is 
weird in this case.

But the intention is for stuff like

short a = 1;

to work without an explicit cast from 1 being an int. So the 
compiler looks at what it can see in the expression - exact 
values of literals, ranges of possible values from other 
variables - and determines the smallest type it can fit inside. 
Then it automatically assigns it that type instead.


What gets pretty weird is if you add a bool overload and pass 
1.... the compiler considers bool to be a very restrained 
integral type... and it knows the values of `enum` too which can 
give some surprising results.

But yeah the values or the possible ranges of values can actually 
affect the types of arithmetic expressions and this is considered 
in overloading for better or for worse.


More information about the Digitalmars-d-learn mailing list