2
我有使用着名的container_of
宏來實現僅宏鏈接列表庫的代碼。C++中的container_of宏具有與C相同的簽名
它完全在C.現在我要支持C++就可以了,所以我需要一個container_of
替代C++,下面的簽名相匹配:
container_of(ptr, type, member)
C實現是這樣的:
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) *__mptr = (ptr);
(type *)((char *)__mptr - offsetof(type,member));})
也許downvoter可以幫我們一個忙,並解釋他們的批評? –
'offsetof'是一個功能不佳的名稱,因爲它是標準庫中的一個宏。 – aschepler
@aschelper這是一個很好的觀點,在實踐中我不會使用這些常用名稱,但將其留爲教學建議的「默認」。無論如何編輯了答案,在名稱上添加了my_前綴以避免碰撞。 –