Skip to content

lib/sizeof.h: typeas(): Add support for 'void' and function types - #1702

Open
alejandro-colomar wants to merge 1 commit into
shadow-maint:masterfrom
alejandro-colomar:typeas
Open

lib/sizeof.h: typeas(): Add support for 'void' and function types#1702
alejandro-colomar wants to merge 1 commit into
shadow-maint:masterfrom
alejandro-colomar:typeas

Conversation

@alejandro-colomar

@alejandro-colomar alejandro-colomar commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

typeas() should be a generic macro that accepts any types that typeof() accepts.

The current implementation, while simple, didn't allow 'void' and function types, since one can't create a compound literal of such a type.

The solution needs to create a pointer type, which accepts a compound literal even for void and function pointee types. This requires using typeof() within the parenthesized type of the compound literal, and thus we need a different way to validate that the input is a type name (not an expression), which is done through _Generic().

Here's an expanded version of it, to help review it:

	#define typeas(T)  typeof                                     \
	(                                                             \
		*(typeof(T) *){                                       \
			_Generic(0, T: NULL, default: NULL)           \
		}                                                     \
	)

For the source code, let's keep it as a one-liner.

BTW, a nice side effect of this implementation is that we use {NULL} instead of {0}, and thus we get rid of -Wzero-as-null-pointer-constant diagnostics when the type T is a pointer type.

This implementation is compatible to GNU C11 --and ISO C23, which standardized typeof()--.

Cc: @uecker, @kees , @chrisbazley


Revisions:

v2
  • Replace some remaining uses of typeof() by typeas(). After this, only two uses of typeof() remain, and they're both strictly necessary (they can't be replaced by typeas()).
$ git rd 
1:  1c42851aafc3 = 1:  1c42851aafc3 lib/sizeof.h: typeas(): Add support for 'void' and function types
-:  ------------ > 2:  c5a6a7adb832 lib/: Use typeas() instead of typeof() where possible
v2b
  • Rebase
$ git rd 
1:  1c42851aafc3 = 1:  8c1ed7af27a5 lib/sizeof.h: typeas(): Add support for 'void' and function types
2:  c5a6a7adb832 = 2:  24c7117e116e lib/: Use typeas() instead of typeof() where possible
v3
  • Drop second commit. It didn't work with old compilers.
$ git rd 
1:  8c1ed7af27a5 = 1:  8c1ed7af27a5 lib/sizeof.h: typeas(): Add support for 'void' and function types
2:  24c7117e116e < -:  ------------ lib/: Use typeas() instead of typeof() where possible

@alejandro-colomar alejandro-colomar self-assigned this Jul 29, 2026
@alejandro-colomar
alejandro-colomar force-pushed the typeas branch 2 times, most recently from c706a2f to 1c42851 Compare July 29, 2026 21:53
typeas() should be a generic macro that accepts any types that
typeof() accepts.

The current implementation, while simple, didn't allow 'void' and
function types, since one can't create a compound literal of such
a type.

The solution needs to create a pointer type, which accepts a compound
literal even for void and function pointee types.  This requires using
typeof() within the parenthesized type of the compound literal, and thus
we need a different way to validate that the input is a type name (not
an expression), which is done through _Generic().

Here's an expanded version of it, to help review it:

	#define typeas(T)  typeof                                     \
	(                                                             \
		*(typeof(T) *){                                       \
			_Generic(0, T: NULL, default: NULL)           \
		}                                                     \
	)

For the source code, let's keep it as a one-liner.

BTW, a nice side effect of this implementation is that we use {NULL}
instead of {0}, and thus we get rid of -Wzero-as-null-pointer-constant
diagnostics when the type T is a pointer type.

This implementation remains compatible to GNU C11 --and ISO C23, which
standardized typeof()--.

Cc: Martin Uecker <uecker@tugraz.at>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
@alejandro-colomar
alejandro-colomar force-pushed the typeas branch 2 times, most recently from 24c7117 to 8c1ed7a Compare July 31, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant