Feature: `static cast`

Nick Treleaven nick at geany.org
Mon Jul 3 17:51:19 UTC 2023


On Tuesday, 6 June 2023 at 05:23:40 UTC, ag0aep6g wrote:
> On 05.06.23 18:06, Quirin Schroll wrote:
>> I don’t think `*cast(D*) &c` is good. I’d use `cast(D) 
>> cast(void*) c`, making unambiguously clear I want a type paint.
>
> `cast(D*) &c` is the unambiguous type paint.

`*cast(D*) &c` (though that requires an lvalue).

> `cast(D) cast(void*) c` can be intercepted by `opCast`.

I tried but it doesn't seem to work:

```d
class C
{
     int* opCast(T)()
     if (is(T == void*)) => new int(42);
}

void main()
{
     C c;
     auto p = cast(void*)c;
     pragma(msg, typeof(p)); // void*
}
```

Unless I've made some mistake above, I don't think intercepting 
`cast(void*)` should be supported. There's probably quite a lot 
of places where people use it to get the class instance pointer. 
Can we change the spec to ignore `opCast!(void*)`?


More information about the Digitalmars-d mailing list