C to D: please help translate this weird macro
Ki Rill
rill.ki at yahoo.com
Thu Sep 21 02:23:32 UTC 2023
On Wednesday, 20 September 2023 at 17:14:41 UTC, Dejan Lekic
wrote:
> [...]
>
> NK_CONTAINER_OF should probably be translated to:
>
> `cast(T*)((cast(void*)ptr - __traits(getMember, T,
> member).offsetof))`
>
> PS. I did not invent this. My original idea was far worse than
> this. - It was suggested on IRC by a much cleverer D programmer
> than myself - Herringway at IRC
Thanks! I translated it to this originally after looking at the
links you've provided:
```D
auto nk_container_of(P, T)(P ptr, T type, size_t member_offsetof)
{
return cast(T*)(cast(void*)(cast(char*)(1 ? (ptr) : ptr -
member_offsetof)));
}
```
> `cast(T*)((cast(void*)ptr - __traits(getMember, T,
> member).offsetof))`
Now, how should I wrap it like a macro? Template mixin? I'm not
that familiar with D meta programming... I shall skim through the
`D templates tutorial` for hints.
More information about the Digitalmars-d-learn
mailing list