cast overly permissive with extern(C++ ) classes; should cast through `void*`

timotheecour timothee.cour2 at gmail.com
Tue Feb 6 21:13:50 UTC 2018


On Tuesday, 6 February 2018 at 20:39:09 UTC, Timothee Cour wrote:
> should we force force casting through `void*` for extern(C++) 
> classes ?
> i.e. `cast(Derived) cast(void*) base_instance;`
>
> currently, `cast(A) unrelad_cpp_instance` happily compiles but 
> shouldn't, it's very error prone even though the cast syntax 
> suggests type safety; especially a problem if we make a class 
> become extern(C++) in the future
>
> [came up after discussing with jacob-carlborg]


https://run.dlang.io/?compiler=dmd&source=import%20std.stdio;%0Aclass%20Foo%20%7B%7D%0A%0Avoid%20main()%0A%7B%0A%09auto%20f%20%3D%20new%20const%20Foo;%0A%20%20%20%20pragma(msg,%20typeof(cast(Foo)%20f));%0A%7D

```
import std.stdio;

extern(C++){
     class Base {void ignore(){}}
class Derived : Base {}
class C {}
}

void main()
{
	Base f = new Derived;
     auto temp=cast(C)f;
     writeln(temp is null); // false, which is not good
}
```


More information about the Digitalmars-d mailing list