This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Yann Droneaud <ydroneaud at opteya dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Tue, 2 Jul 2019 09:32:01 +0100
- Subject: Re: alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field
- References: <dac00bd0f08f74c2c591402ecb0bb65dc28d6a84.camel@opteya.com>
On Tue, 2 Jul 2019 at 08:57, Yann Droneaud <ydroneaud@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)