File: _Generic.3

package info (click to toggle)
manpages 6.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,808 kB
  • sloc: sh: 503; python: 222; perl: 165; makefile: 27; lisp: 22
file content (64 lines) | stat: -rw-r--r-- 1,550 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
.\" Copyright (C) 2022 Alejandro Colomar <alx@kernel.org>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH _Generic 3 2024-06-15 "Linux man-pages (unreleased)"
.SH NAME
_Generic \- type-generic selection
.SH SYNOPSIS
.nf
.BR _Generic( \fIexpression\fP ", type1: " e1 ", " "... /*" \
", default: " "e */" );
.fi
.SH DESCRIPTION
.BR _Generic ()
evaluates the path of code under the type selector
that is compatible with the type of the controlling
.IR expression ,
or
.B default:
if no type is compatible.
.P
.I expression
is not evaluated.
.P
This is especially useful for writing type-generic macros,
that will behave differently depending on the type of the argument.
.SH STANDARDS
C11.
.SH HISTORY
C11.
.SH EXAMPLES
The following program demonstrates how to write
a replacement for the standard
.BR imaxabs (3)
function, which being a function can't really provide what it promises:
seamlessly upgrading to the widest available type.
.IP
.\" SRC BEGIN (_Generic.c)
.EX
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
\&
#define my_imaxabs  _Generic(INTMAX_C(0),  \[rs]
    long:           labs,                  \[rs]
    long long:      llabs                  \[rs]
/*  long long long: lllabs */              \[rs]
)
\&
int
main(void)
{
    off_t  a;
\&
    a = \-42;
    printf("imaxabs(%jd) == %jd\[rs]n", (intmax_t) a, my_imaxabs(a));
    printf("&imaxabs == %p\[rs]n", &my_imaxabs);
    printf("&labs    == %p\[rs]n", &labs);
    printf("&llabs   == %p\[rs]n", &llabs);
\&
    exit(EXIT_SUCCESS);
}
.EE
.\" SRC END