C++ forbids declaration of ‘typeof’ with no type

這個錯誤有點鬧心,因爲 C++11 把 關鍵字typeof改爲decltype了。

怎麼做好兩者的兼容性呢,只能使用 if 宏判斷來做,比如我的代碼:

#if __cplusplus >= 201103L
#define container_of(ptr, type, member) ({          \
        const decltype(((type *)0)->member)*__mptr = (ptr);    \
    (type *)((char *)__mptr - offsetof_lh(type, member)); })
#else
#define container_of(ptr, type, member) ({          \
        const typeof(((type *)0)->member)*__mptr = (ptr);    \
    (type *)((char *)__mptr - offsetof_lh(type, member)); })
#endif 

參考:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章