On Mon, 19 Sep 2011 13:23:32 -0400, bearophile <bearophileh...@lycos.com>
wrote:
This is a simplified version of some older D code of mine.
The cast is needed to convert int->uint:
uint foo(T)(T x) if (is(T == uint)) {
uint ans = 0;
while (x >>= 1)
ans++;
return ans;
}
void bar(int x) {
auto xx = foo(cast(uint)x);
}
void main() {}
I have "improved" it adding a nice "in" in the bar() signature. The code
compiles and runs still:
uint foo(T)(T x) if (is(T == uint)) {
uint ans = 0;
while (x >>= 1)
ans++;
return ans;
}
void bar(in int x) {
auto xx = foo(cast(uint)x);
}
void main() {}
Unfortunately now the code contains a small bug. The cast now converts
const(int)->uint, and foo() will modify a const value, that gives
undefined effects in D2.
While I agree D2 needs a const_cast, this code is still correct. uints
are passed by value, so it is fine what you did.
I'd say to avoid needing the cast, you should instead change bar to this:
auto xx = foo!uint(x);
which should work just fine, no cast needed.
-Steve