D 2.066 new behavior
Paul D Anderson via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Thu Aug 21 18:54:53 PDT 2014
In all previous versions through 2.066 beta 5, the following code
compiled and ran correctly:
import std.stdio;
T add(T)(in T x, in T y)
{
T z;
z = x + y;
return z;
}
void main()
{
const double a = 1.0;
const double b = 2.0;
double c;
c = add(a,b);
writefln("c = %s", c); // 3.0
c = 1.0;
writefln("c = %s", c); // 1.0
}
From beta 6 onward it no longer compiles. The problem seems to be
const qualifiers being carried into the template type. Since a
and b are const double, the function template parameter T is
const double. So x and y are const, no problem, but z is now
const also. The following error message is given.
T add(T)(in T x, in T y)
{
T z;
z = x + y; // Error: Can't modify const expression z
return z;
}
The same problem shows up elsewhere as 'cannot implicitly convert
const x to x'
and 'none of the overloads are callable using argument types (x)
const'.
Is this expected behavior that has never been enforced before, or
is it something new?
And is anyone else having the same problem?
Paul
More information about the Digitalmars-d-announce
mailing list