typeof_unqual is a C23 type operator that derives the unqualified version of the type produced by an expression or type name. It belongs to C23 type inference alongside auto and typeof, helping generic code avoid repeating type-specific cases. section 18.0 section 18.3.2
A practical use is a macro that accepts an expression, calls a fixed implementation, and casts the result to that expression's unqualified type. The expression can therefore determine the result type without requiring a typedef or a separately written type name. section 18.3.2
Preserving the expression's unqualified type
static inline long double absolute(long double absolute_x) { return absolute_x < 0.0L ? -absolute_x : absolute_x; } #define absolute(X) ((typeof_unqual(X))absolute(X)) Here, typeof_unqual(X) supplies the cast target while absolute(X) performs the calculation. The operator is useful when qualifiers must not be carried into the macro's result type. section 18.3.2
Combining type inference with generic selection
#define pow(X, Y) \ ((typeof_unqual((X)+(Y)+0.0F)) \ _Generic((Y)+0ULL, \ unsigned long long: pownl, \ default: powl) \ ((X), (Y))) This pattern uses typeof_unqual to determine the result type and _Generic to choose an implementation according to the second argument. section 18.3.2
Use typeof_unqual with these constraints:
- Prefer auto for ordinary variable declarations; typeof and typeof_unqual are intended for more sophisticated type-generic cases. section 18.3.2
- Combine it with _Generic when a macro must infer a type or select an implementation without duplicating fixed type cases. section 18.0 section 18.3.2
