public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field
@ 2019-07-02  7:57 Yann Droneaud
  2019-07-02  8:32 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Yann Droneaud @ 2019-07-02  7:57 UTC (permalink / raw)
  To: gcc

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 ? 

Is this possible ? Is such feature was already considered ?

Regards.

-- 
Yann Droneaud
OPTEYA


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field
  2019-07-02  7:57 alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field Yann Droneaud
@ 2019-07-02  8:32 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2019-07-02  8:32 UTC (permalink / raw)
  To: Yann Droneaud; +Cc: gcc

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)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-07-02  8:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-02  7:57 alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field Yann Droneaud
2019-07-02  8:32 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).