Linux Manual Pages development
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: linux-man@vger.kernel.org
Cc: Alejandro Colomar <alx.manpages@gmail.com>,
	JeanHeyd Meneide <wg14@soasis.org>,
	"G. Branden Robinson" <g.branden.robinson@gmail.com>,
	Ingo Schwarze <schwarze@usta.de>
Subject: [PATCH] _Generic.3: New page documenting _Generic()
Date: Sat, 20 Aug 2022 19:10:10 +0200	[thread overview]
Message-ID: <20220820171009.34196-1-alx.manpages@gmail.com> (raw)

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@gmail.com>
Cc: JeanHeyd Meneide <wg14@soasis.org>
Cc: "G. Branden Robinson" <g.branden.robinson@gmail.com>
Cc: Ingo Schwarze <schwarze@usta.de>
---

Hi,

This page is one of the first using true-case page title, and the
first one using the new Linux man-pages 4th .TH argument.

I always forget about the syntax details of using _Generic(),
since it's different to anything else (why didn't they just use
switch-case syntax??!).

I hope this is useful for others as it is for me.

Cheers,

Alex


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

diff --git a/man3/_Generic.3 b/man3/_Generic.3
new file mode 100644
index 000000000..7c88a6975
--- /dev/null
+++ b/man3/_Generic.3
@@ -0,0 +1,69 @@
+.\" Copyright (C) 2022 Alejandro Colomar <alx.manpages@gmail.com>
+.\"
+.\" 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 \e
+((intmax_t) 0,                  \e
+                                \e
+    int:                        \e
+        abs(j),                 \e
+                                \e
+    long:                       \e
+        labs(j),                \e
+                                \e
+    long long:                  \e
+        llabs(j)                \e
+                                \e
+    /* long long long: */       \e
+    /*  lllabs(j)     */        \e
+)
+
+int
+main(void)
+{
+    short a;
+
+    a = \-42;
+    printf("imaxabs(%d) == %jd\en", a, my_imaxabs(a));
+
+    exit(EXIT_SUCCESS);
+}
+.EE
+.\" SRC END
-- 
2.37.2


             reply	other threads:[~2022-08-20 17:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-20 17:10 Alejandro Colomar [this message]
2022-08-21 11:55 ` [PATCH v2] _Generic.3: New page documenting _Generic() Alejandro Colomar
2022-08-23  7:58   ` Florian Weimer
2022-08-23 13:16     ` Alejandro Colomar
2022-08-23 13:24       ` Alejandro Colomar
2022-08-23 13:32     ` Alejandro Colomar
2022-08-23 13:34       ` Alejandro Colomar
2022-09-04 21:16     ` Alejandro Colomar
2022-09-04 22:06       ` Alejandro Colomar
2022-09-09 12:31       ` Alejandro Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220820171009.34196-1-alx.manpages@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=g.branden.robinson@gmail.com \
    --cc=linux-man@vger.kernel.org \
    --cc=schwarze@usta.de \
    --cc=wg14@soasis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox