4 ms·
FieldParentPtr
- tleb_ 3y agoThis is usually called container_of in C projects. It is compile-time safe as well, though the error won't be pretty. Linux, Qemu, musl, U-Boot, iproute2 & uclibc-ng each have a copy from the few project I've looked at. Two interesting implementations: - Linux, compile-time checks that ptr is the same type as the member: https://elixir.bootlin.com/linux/v6.6/source/include/linux/container_of.h#L18 https://elixir.bootlin.com/linux/v6.6/source/include/linux/c... - musl, minimal: https://elixir.bootlin.com/musl/v1.2.4/source/ldso/dynlink.c#L45 https://elixir.bootlin.com/musl/v1.2.4/source/ldso/dynlink.c...