| 1 |
.\" Copyright, the authors of the Linux man-pages project |
| 2 |
.\" |
| 3 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 4 |
.\" |
| 5 |
.TH _Generic 3 2025-05-17 "Linux man-pages 6.18" |
| 6 |
.SH NAME |
| 7 |
_Generic \- type-generic selection |
| 8 |
.SH SYNOPSIS |
| 9 |
.nf |
| 10 |
.BR _Generic( \f[I]expression\f[] ", type1: " \f[I]e1\f[] ", " "... /*" \ |
| 11 |
", default: " "\f[I]e\f[] */" ); |
| 12 |
.fi |
| 13 |
.SH DESCRIPTION |
| 14 |
.BR _Generic () |
| 15 |
evaluates the path of code under the type selector |
| 16 |
that is compatible with the type of the controlling |
| 17 |
.IR expression , |
| 18 |
or |
| 19 |
.B default: |
| 20 |
if no type is compatible. |
| 21 |
.P |
| 22 |
.I expression |
| 23 |
is not evaluated. |
| 24 |
.P |
| 25 |
This is especially useful for writing type-generic macros, |
| 26 |
that will behave differently depending on the type of the argument. |
| 27 |
.SH STANDARDS |
| 28 |
C11. |
| 29 |
.SH HISTORY |
| 30 |
C11. |
| 31 |
.SH EXAMPLES |
| 32 |
The following program demonstrates how to write |
| 33 |
a replacement for the standard |
| 34 |
.BR imaxabs (3) |
| 35 |
function, which being a function can't really provide what it promises: |
| 36 |
seamlessly upgrading to the widest available type. |
| 37 |
.IP |
| 38 |
.\" SRC BEGIN (_Generic.c) |
| 39 |
.EX |
| 40 |
#include <stdint.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <stdlib.h> |
| 43 |
\& |
| 44 |
#define my_imaxabs _Generic(INTMAX_C(0), \[rs] |
| 45 |
long: labs, \[rs] |
| 46 |
long long: llabs \[rs] |
| 47 |
/* long long long: lllabs */ \[rs] |
| 48 |
) |
| 49 |
\& |
| 50 |
int |
| 51 |
main(void) |
| 52 |
{ |
| 53 |
off_t a; |
| 54 |
\& |
| 55 |
a = \-42; |
| 56 |
printf("imaxabs(%jd) == %jd\[rs]n", (intmax_t) a, my_imaxabs(a)); |
| 57 |
printf("&imaxabs == %p\[rs]n", &my_imaxabs); |
| 58 |
printf("&labs == %p\[rs]n", &labs); |
| 59 |
printf("&llabs == %p\[rs]n", &llabs); |
| 60 |
\& |
| 61 |
exit(EXIT_SUCCESS); |
| 62 |
} |
| 63 |
.EE |
| 64 |
.\" SRC END |