liveBook · Manning

_Generic

As explained in Modern C

_Generic is a C11 primary expression that selects one expression from type-associated alternatives based on the type of a controlling expression.

_Generic is a C11 primary expression for type-generic programming. It examines the type of a controlling expression, then selects one expression associated with that type or with default. Although it is not itself a macro feature, it fits naturally inside function-like macro interfaces. section 16.6

1   _Generic(controlling expression,
2     type1: expression1,
3     ... ,
4     typeN: expressionN)


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":1,\"ch\":10},{\"line\":1,\"ch\":11}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}]]"}
!@%STYLE%@!

The form lists a controlling expression followed by type-to-expression associations; one association may use default. section 16.6

Selecting a type-specific function

A type-generic fabs interface can select fabsf for float, fabsl for long double, and fabs otherwise: #define fabs(X) \ _Generic((X), \ float: fabsf, \ long double: fabsl, \ default: fabs)(X) The selected expression is a function pointer. The following (X) applies that function to the argument. Other arithmetic types, including double and integer types, use the default association. section 16.6

Combining argument types

A floating-point minimum interface defines min, minl, and minf, then uses _Generic((A)+(B), ...) to select among them. Using the sum as the controlling expression lets the usual arithmetic conversions determine the relevant combination of the two argument types; the selected function is then called with (A), (B). section 16.6

TAKEAWAY: _Generic chooses by type, not by a runtime value. This makes it useful for presenting a simple macro interface while dispatching to different functions or expressions. More elaborate interfaces can also use it to choose format strings or conversions for tracing. section 16.6

How Modern C covers it

How deep it goes
Defines syntax and dispatches complete macro examples, then stops short of broader macro-programming complications.
Examples
Demonstrates fabs dispatch, floating-point minimum, maximum-value selection, and tracing format or value conversion.
What it assumes
Assumes familiarity with C expressions and function-like macros while introducing type-generic programming directly in C11.
What you can do after
Apply _Generic through macros to select type-specific functions, expressions, format strings, or conversions.
How it connects
Connects type-generic programming with function-like macros, inline functions, function pointers, and usual arithmetic conversions.

Also explained in

Compare how 2 books explain _Generic →

Related concepts

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