C ++中的`container_of`宏,其签名与C相同

Fel*_*tti 5 c++

我有使用著名的container_of宏来实现仅宏链接列表库的代码。

它可以完美地在C语言中运行。现在,我想在它上面支持C ++,因此我需要一个container_of与以下签名匹配的C ++替代品:

container_of(ptr, type, member)
Run Code Online (Sandbox Code Playgroud)

C实现是这样的:

#define container_of(ptr, type, member) ({ \
            const typeof( ((type *)0)->member ) *__mptr = (ptr); 
            (type *)( (char *)__mptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)

Fel*_*tti 5

为自己量身定制了解决方案。没有模板会更好:

template<class P, class M>
size_t my_offsetof(const M P::*member)
{
    return (size_t) &( reinterpret_cast<P*>(0)->*member);
}

template<class P, class M>
P* my_container_of_impl(M* ptr, const M P::*member)
{
    return (P*)( (char*)ptr - my_offsetof(member));
}

#define my_container_of(ptr, type, member) \
     my_container_of_impl (ptr, &type::member)
Run Code Online (Sandbox Code Playgroud)

因为,在 C 中,我们通常使用typeofwithcontainer_of来获取变量的类型,例如:

typedef struct _AStruct
{
    int data_field;
} AStruct;

AStruct as;
int * ptr = &as.data_field;

my_container_of(ptr, AStruct, data_field);
my_container_of(ptr, typeof(as), data_field);
Run Code Online (Sandbox Code Playgroud)

我们也可以提供一个额外的宏来实现typeof等价:

#include <type_traits>
#define my_typeof(___zarg) std::remove_reference<decltype(___zarg)>::type 
Run Code Online (Sandbox Code Playgroud)