summaryrefslogtreecommitdiffstats
path: root/man/man3/_Generic.3
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-24 04:52:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-24 04:52:22 +0000
commit3d08cd331c1adcf0d917392f7e527b3f00511748 (patch)
tree312f0d1e1632f48862f044b8bb87e602dcffb5f9 /man/man3/_Generic.3
parentAdding debian version 6.7-2. (diff)
downloadmanpages-3d08cd331c1adcf0d917392f7e527b3f00511748.tar.xz
manpages-3d08cd331c1adcf0d917392f7e527b3f00511748.zip
Merging upstream version 6.8.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'man/man3/_Generic.3')
-rw-r--r--man/man3/_Generic.364
1 files changed, 64 insertions, 0 deletions
diff --git a/man/man3/_Generic.3 b/man/man3/_Generic.3
new file mode 100644
index 0000000..f8e8b1b
--- /dev/null
+++ b/man/man3/_Generic.3
@@ -0,0 +1,64 @@
+.\" Copyright (C) 2022 Alejandro Colomar <alx@kernel.org>
+.\"
+.\" SPDX-License-Identifier: Linux-man-pages-copyleft
+.\"
+.TH _Generic 3 2024-05-02 "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), \e
+ long: labs, \e
+ long long: llabs \e
+/* long long long: lllabs */ \e
+)
+\&
+int
+main(void)
+{
+ off_t a;
+\&
+ a = \-42;
+ printf("imaxabs(%jd) == %jd\en", (intmax_t) a, my_imaxabs(a));
+ printf("&imaxabs == %p\en", &my_imaxabs);
+ printf("&labs == %p\en", &labs);
+ printf("&llabs == %p\en", &llabs);
+\&
+ exit(EXIT_SUCCESS);
+}
+.EE
+.\" SRC END