[PATCH v2] _Generic.3: New page documenting _Generic()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Also add a hint of how intmax(3) and other functions using
[u]intmax_t types could be better defined by ISO C, by requiring
that they're implemented as type-generic macros, to avoid having
problems with the ABI.

Signed-off-by: Alejandro Colomar <alx.manpages@xxxxxxxxx>
Cc: JeanHeyd Meneide <wg14@xxxxxxxxxx>
Cc: "G. Branden Robinson" <g.branden.robinson@xxxxxxxxx>
Cc: Ingo Schwarze <schwarze@xxxxxxx>
---

v2:

- Use a type that really needs to be converted to (intmax_t).
- Compatc _Generic().

 man3/_Generic.3 | 60 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 man3/_Generic.3

diff --git a/man3/_Generic.3 b/man3/_Generic.3
new file mode 100644
index 000000000..3e1c159ab
--- /dev/null
+++ b/man3/_Generic.3
@@ -0,0 +1,60 @@
+.\" Copyright (C) 2022 Alejandro Colomar <alx.manpages@xxxxxxxxx>
+.\"
+.\" SPDX-License-Identifier: Linux-man-pages-copyleft
+.\"
+.TH _Generic 3 2022-08-20 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
+.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.
+.PP
+.I expression
+is not evaluated.
+.PP
+This is especially useful for writing type-generic macros,
+that will behave differently depending on the type of the argument.
+.SH STANDARDS
+C11 and later.
+.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.
+.PP
+.\" SRC BEGIN (_Generic.c)
+.EX
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define my_imaxabs(j)  _Generic((intmax_t) 0,  \e
+    int:            abs(j),                    \e
+    long:           labs(j),                   \e
+    long long:      llabs(j)                   \e
+ /* long long long: lllabs(j) */               \e
+)
+
+int
+main(void)
+{
+    off_t a;
+
+    a = \-42;
+    printf("imaxabs(%jd) == %jd\en", (intmax_t) a, my_imaxabs(a));
+
+    exit(EXIT_SUCCESS);
+}
+.EE
+.\" SRC END
-- 
2.37.2




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux