This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field
- From: Yann Droneaud <ydroneaud at opteya dot com>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 02 Jul 2019 09:57:28 +0200
- Subject: alignof(type, field); sizeof(type, field); typeof(type, field): getting type information on nested field
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