Masalkhi
Masalkhi

Reputation: 131

What this mean in C ((TYPE){}). That fragment is taken from : __typeof__ ( ((TYPE){}).MEMBER )

#define _IO_MEMBER_TYPE (type, member)       \
__typeof__ ( ((TYPE){}).MEMBER )

I have read this line in the header file "libiop.h" in the glibc and I got a bit confused about the curly brackets {} after (TYPE). What does ( (TYPE){} ) mean?

Upvotes: 2

Views: 154

Answers (2)

John Bollinger
John Bollinger

Reputation: 180316

What does ( (TYPE){} ) mean?

It is not standard C, but it almost has the form of a C99 compound literal of type TYPE (enclosed in parentheses), which would be appropriate to the apparent purpose. It is non-standard because The brace-enclosed part of a compound literal must take the same form as an initializer for the designated type, and C does not permit empty initializers. This variation would be fully standard whenever TYPE designates a structure, union, or array type:

((TYPE){0})

__typeof__ also is non-standard, but it has the form of an identifier reserved for the implementation's use, so it is undoubtedly an implementation-specific extension, as one would presume accepting an empty initializer list is also.

Upvotes: 2

Florian Weimer
Florian Weimer

Reputation: 33719

The macro denotes the type of a specific struct member. The comment tries to explain this:

/* Type of MEMBER in struct type TYPE.  */
#define _IO_MEMBER_TYPE(TYPE, MEMBER) __typeof__ (((TYPE){}).MEMBER)

The macro is only used here:

/* Essentially ((TYPE *) THIS)->MEMBER, but avoiding the aliasing
   violation in case THIS has a different pointer type.  */
#define _IO_CAST_FIELD_ACCESS(THIS, TYPE, MEMBER) \
  (*(_IO_MEMBER_TYPE (TYPE, MEMBER) *)(((char *) (THIS)) \
  + offsetof(TYPE, MEMBER)))

This construct uses various GCC extensions to implement C++-style class inheritance. The direct way of writing this no longer works (or triggers warnings) with recent GCC versions. (The libio code and the C++ ABI it implements date back to GCC 2.95 in the 90s.)

This code is quite bad and you really should not use it as a model for anything.

Upvotes: 2

Related Questions