C to D: please help translate this weird macro

Ki Rill rill.ki at yahoo.com
Fri Sep 22 12:24:48 UTC 2023


On Thursday, 21 September 2023 at 16:28:25 UTC, Nick Treleaven 
wrote:
>
> The 1st argument of `getMember` can just be T, like the 
> original macro.
> The 2nd argument needs to be a compile-time string.
> Also the `char*` cast needs to apply to `ptr` before 
> subtracting the offset AFAICS.
>
> So reordering to keep type inference of `ptr`:
> ```d
> auto nk_container_of(T, string member, P)(P ptr)
> {
>     return cast(T*)(cast(void*)(cast(char*)ptr -
>         __traits(getMember, T, member).offsetof)));
> }
> ```
> (Untested)

I will test it:)


More information about the Digitalmars-d-learn mailing list