size_t index=-1;

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 17 15:46:01 PDT 2016


On Thursday, 17 March 2016 at 17:09:46 UTC, Steven Schveighoffer 
wrote:
> Converting unsigned to signed or vice versa (of the same size 
> type) is safe. No information is lost.

Saying that "no information is lost" in such a case, is like 
saying that if I encrypt my hard drive and then throw away the 
password, "no information is lost". Technically this is true: the 
bit count is the same as it was before.

In practice, though, the knowledge of how information is encoded 
is essential to actually using it.

In the same way, using `cast(ulong)` to pass `-1L` to a function 
that expects a `ulong` results in a de-facto loss of information, 
because that `-1L` can no longer distinguished from `ulong.max`, 
despite the fundamental semantic difference between the two.

> VRP on steroids would be nice, but I don't think it's as 
> trivial to solve.

D's current VRP is actually surprisingly anemic: it doesn't even 
understand integer comparisons, or the range restrictions implied 
by the predicate when a certain branch of an `if` statement is 
taken.

Lionello Lunesu made a PR a while back that adds these two 
features, and it makes the compiler feel a lot smarter. (The PR 
was not accepted at the time, but I have since revived it:
     https://github.com/D-Programming-Language/dmd/pull/5229)


More information about the Digitalmars-d-learn mailing list