Comment #0 by thomas.bockman — 2015-12-06T04:08:02Z
This code:
N n;
M m;
n ^^ = m;
Seems to be lowered to this:
N n;
M m;
n = pow(n, m);
For many numeric type combinations, this will fail to compile, yielding "Error: cannot implicitly convert expression (pow(cast(N)n, cast(M)m)) of type R to N", where R is some other numeric type.
In particular, operator ^^= is completely unusable with byte, ubyte, short, and ushort, since for those types pow returns int, which cannot be implicitly converted to a lesser type.
The fix is to include an explicit cast in the lowering:
N n;
M m;
n = cast(N) pow(n, m);
This would make the behaviour of ^^= consistent with the other assignment operators (like *=) which do not have this problem.
// Full test code ///////////////////////////////////
import std.traits : NumericTypeList;
import std.stdio;
void main(string[] args) {
foreach(N; NumericTypeList) {
foreach(M; NumericTypeList) {
N n = 1;
M m = 1;
if(!__traits(compiles, n += m))
writeln(N.stringof ~ " ^^= " ~ M.stringof ~ " fails to compile.");
}
}
}
Comment #1 by robert.schadek — 2024-12-13T18:46:01Z