[1/2] add typeof_member() macro
diff mbox series

Message ID 20190529190720.GA5703@avx2
State Accepted
Commit ce251e0e3c0597ea8cab5787df579bd1f9c1aca1
Headers show
Series
  • [1/2] add typeof_member() macro
Related show

Commit Message

Alexey Dobriyan May 29, 2019, 7:07 p.m. UTC
Add typeof_member() macro so that types can be exctracted without
introducing dummy variables.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 include/linux/kernel.h |    2 ++
 1 file changed, 2 insertions(+)

Comments

David Laight May 30, 2019, 11:37 a.m. UTC | #1
From: Alexey Dobriyan
> Sent: 29 May 2019 20:07
> 
> Add typeof_member() macro so that types can be exctracted without
> introducing dummy variables.
> 
> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
> ---
> 
>  include/linux/kernel.h |    2 ++
>  1 file changed, 2 insertions(+)
> 
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -88,6 +88,8 @@
>   */
>  #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
> 
> +#define typeof_member(T, m)	typeof(((T*)0)->m)

Should probably be 't' (not 'T') and upper case ?

Hmmm.... the #define is longer that what it expands to ...

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Joe Perches May 30, 2019, 11:53 a.m. UTC | #2
On Thu, 2019-05-30 at 11:37 +0000, David Laight wrote:
> From: Alexey Dobriyan
> > Sent: 29 May 2019 20:07
> > 
> > Add typeof_member() macro so that types can be exctracted without
> > introducing dummy variables.
> > 
> > Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
> > ---
> > 
> >  include/linux/kernel.h |    2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > --- a/include/linux/kernel.h
> > +++ b/include/linux/kernel.h
> > @@ -88,6 +88,8 @@
> >   */
> >  #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
> > 
> > +#define typeof_member(T, m)	typeof(((T*)0)->m)

>
> Should probably be 't' (not 'T') and upper case ?
> 
> Hmmm.... the #define is longer that what it expands to ...

While I did object to the avoidance in the obvious
misnaming of FIELD_SIZEOF, this could reasonably
be named FIELD_TYPEOF for symmetry.
Alexey Dobriyan May 30, 2019, 7:57 p.m. UTC | #3
On Thu, May 30, 2019 at 11:37:42AM +0000, David Laight wrote:
> From: Alexey Dobriyan

> > +#define typeof_member(T, m)	typeof(((T*)0)->m)
> 
> Should probably be 't' (not 'T') and upper case ?

T comes from C++ where they uppercase types in template arguments.
It makes sense as types stand out sligthly around lowercase identifiers.

> Hmmm.... the #define is longer that what it expands to ...

It hides cast of 0 which is implementation detail :^)

Proper syntax would be

	typeof(struct foo::bar)

but one can only dream.

Patch
diff mbox series

--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -88,6 +88,8 @@ 
  */
 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
 
+#define typeof_member(T, m)	typeof(((T*)0)->m)
+
 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
 
 #define DIV_ROUND_DOWN_ULL(ll, d) \