Bug 14331 – Cannot cast(void*) a class with an alias this struct
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2015-03-25T01:56:10Z
Last change time
2018-10-29T12:08:46Z
Assigned to
No Owner
Creator
Alex Parrill
Comments
Comment #0 by initrd.gz — 2015-03-25T01:56:10Z
This code:
class Test {
struct Foo {
int a, b, c;
}
Foo foo;
alias foo this;
}
void main() {
auto test = new Test();
destroy(test);
}
Fails to compile with the following message:
$ rdmd test.d
c:\D\dmd2\windows\bin\..\..\src\druntime\import\object.di(523): Error: cannot cast from Foo to void*
test.d(13): Error: template instance object.destroy!(Test) error instantiating
Failed: ["dmd", "-v", "-o-", "test.d", "-I."]
Presumably because the `cast(void*)` is trying to cast the struct that is aliased instead of the class. Removing the `alias foo this` causes the code to compile.
Using version 2.066.1 at the moment, but I didn't see anything related in the changelog for v2.067.
Comment #1 by code — 2015-09-11T21:48:28Z
cat > bug.d << CODE
class Test
{
static struct S {}
S s;
alias s this;
}
void main()
{
auto p = cast(void*)(new Test);
}
CODE
dmd -c bug
----
bug2.d(11): Error: cannot cast expression (new Test).s of type S to void*
----
It is a more general problem that you cannot cast the class to void*.
Comment #2 by razvan.nitu1305 — 2018-10-29T12:08:46Z
*** This issue has been marked as a duplicate of issue 6777 ***