g++使用container_of编译不通过

Linux内核里面大量使用了container_of,使用gcc编译完全不存在问题;但项目中刚好需要使用g++进行编译,container_of一直编译不通过,通过使用

g++ -E source.cpp >> test.cpp

一步一步测试,最后修改如下:

#ifdef __cplusplus
#define container_of(ptr, type, member) ({ \
        typeof(((type *)0)->member)*__mptr = ptr; \
        (type *)((char *)__mptr - offsetof(type, member)); })
#else
#define container_of(ptr, type, member) ({ \
        typeof(((type *)0)->member)(*__mptr) = ((void *)ptr); \
        (type *)((char *)__mptr - offsetof(type, member)); })

#endif

由于对C++不熟,还是不是很明白(*__mptr)为什么编译不通过,有时间在继续深究原因。

posted @ 2017-10-31 20:56  伤心中国人  阅读(369)  评论(0编辑  收藏  举报