liveBook · Manning

Typeof_unqual

As explained in Modern C, Third Edition

typeof_unqual is a C23 type operator that derives the unqualified version of a type from an expression or type name.

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

How Modern C, Third Edition covers it

How deep it goes
Explains the operator through result-casting and generic-selection macros, while deferring a complete type-system treatment.
Examples
Uses absolute and pow macros to demonstrate unqualified result casting and implementation selection.
What it assumes
Assumes familiarity with C expressions, types, qualifiers, casts, macros, and _Generic; contrasts with simpler auto declarations.
What you can do after
Apply it via C23 macros that infer result types and cooperate with _Generic.
How it connects
Connects typeof_unqual with C23 type inference, auto, typeof, _Generic, macros, and type compatibility.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.