On Tue, 2 Jul 2019 at 08:57, Yann Droneaud <ydrone...@opteya.com> wrote:
>
> Hi,
>
> I'm sometime in need to "probe" the size, the type, (and less often the
> alignment) of a field inside a structure.
>
> In such case I have to write "ugly" thing like
>
>   struct A
>   {
>     struct
>     {
>       type_t t;
>     } B;
>   };
>
>   typeof(((struct A *)NULL)->B.t) V;
>
> It would have been some much pleasing to have 2 parameters version of
> sizeof(), typeof(), and alignof(), which would look like a lot like
> offsetof() usage:
>
>   typeof(struct A, B.t) V;
>
>   if (sizeof(struct A, B.t) != sizeof(long)) ... ;
>
>   if (alignof(struct A, B.t) < alignof(long)) ... ;
>
> As sizeof is an operator, this is not straightforward to implement.
>
> Currently sizeof (0, variable); is a valid construct, giving the size
> of  "variable".
>
> Hence, turning sizeof() to a variadic function-like thing would break
> existing code.
>
> Maybe it would possible to add such function-like operator as _Sizeof()
> ? And have a header #define'ing sizeof(...) as _Sizeof() for code
> wanting the new behavior ?

That seems like a terrible idea.

> Is this possible ? Is such feature was already considered ?

#define SIZEOF_MEM(type, member) sizeof(((type*)0).member)
#define TYPEOF_MEM(type, member) __typeof__(((type*)0).member)
#define ALIGNOF_MEM(type, member) _Alignof(((type*)0).member)

Reply via email to