lib/sizeof.h: typeas(): Add support for 'void' and function types - #1702
Open
alejandro-colomar wants to merge 1 commit into
Open
lib/sizeof.h: typeas(): Add support for 'void' and function types#1702alejandro-colomar wants to merge 1 commit into
alejandro-colomar wants to merge 1 commit into
Conversation
alejandro-colomar
force-pushed
the
typeas
branch
2 times, most recently
from
July 29, 2026 21:53
c706a2f to
1c42851
Compare
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
force-pushed
the
typeas
branch
2 times, most recently
from
July 31, 2026 16:51
24c7117 to
8c1ed7a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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
v2b
v3