Bug 7143 – [CTFE] cannot compare class references with "is"
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-12-20T06:58:00Z
Last change time
2011-12-21T15:24:10Z
Assigned to
nobody
Creator
r.sagitario
Comments
Comment #0 by r.sagitario — 2011-12-20T06:58:20Z
Comparing class references with null or other classes causes an error in CTFE, e.g.:
module test;
class C
{
int x;
}
int getA()
{
C c = new C;
if(c is null)
return -1;
return c.x;
}
static assert(getA() == 0);
Compiling with "dmd -c test.d" yields:
test.d(11): Error: cannot compare C(0) at compile time
test.d(16): called from here: getA()
test.d(16): while evaluating: static assert(getA() == 0)
if you use "if(c)" instead of "if(c is null)", the error itself disappears, there is only the call stack left:
test.d(16): called from here: getA()
test.d(16): while evaluating: static assert(getA() == 0)
Using "if(!c)" seems to work.