emacs-devel
[Top][All Lists]
Advanced

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

Re: master 37889523278: Add new `swap` macro and use it


From: Po Lu
Subject: Re: master 37889523278: Add new `swap` macro and use it
Date: Sun, 07 Jan 2024 18:36:07 +0800
User-agent: Gnus/5.13 (Gnus v5.13)

Stefan Kangas <stefankangas@gmail.com> writes:

> 2. "the first argument can easily become out of date without notice"
>
>     This is true, but of course only when _GL_HAVE___TYPEOF__.
>
>     When !_GL_HAVE___TYPEOF__,
>
>         swap (T, x, y);
>
>     is subject to the same basic type errors as
>
>         T tmp = x; x = y; y = tmp;
>
>     If that is not workable, we could, as a last resort, just forego
>     using typeof altogether.  But at that point, the macro is reduced to
>     be mostly aesthetic.

There are no "basic type errors" inherent in this statement.  In most
instances where the type of a variable is modified, it is from one
integer type to another (e.g. long to ptrdiff_t), which means
inconsistencies between the type in the swap statement and the values
being swapped might result in truncation on compilers where __typeof__
is absent.  As they do not emit diagnostics in response to such errors,
bugs so introduced won't appear until the erroneous code is in fact
executed and the values involved are sufficiently large that truncation
produces a visible malfunction; worse yet, such bugs cannot be
discovered by developers using GCC.

> So no, I don't currently see any evidence for the claims that this
> "cannot be fixed" or is "impossible to implement in C".  If there is any
> substance to that, you will probably have to explain it again.

This has been axiomatic for eons.  From the comp.lang.c FAQ:

10.3: How can I write a generic macro to swap two values?

A: There is no good answer to this question.  If the values are
integers, a well-known trick using exclusive-OR could perhaps be used,
but it will not work for floating-point values or pointers, or if the
two values are the same variable.  (See questions 3.3b and 20.15c.)  If
the macro is intended to be used on values of arbitrary type (the usual
goal), it cannot use a temporary, since it does not know what type of
temporary it needs (and would have a hard time picking a name for it if
it did), and standard C does not provide a typeof operator.

The best all-around solution is probably to forget about using a macro,
unless you're willing to pass in the type as a third argument.

> Now, as I have already explained, the macro is certainly less
> subjectively appealing if we have to manually write out the type every
> time.  In my view it is still a bit better, since we get both more
> readable code and additional safety on our main targets.  (Note that we
> don't currently use -Wconversion in Emacs.)

Never in my life have I heard the statements for swapping two variables
characterized as "insufficiently readable", and the GNU Coding Standards
discourage programming for -Wconversion, with good reason: for each
potential error it might detect, there are numerous more assignments
where truncation is harmless or even correct, and ironically it is
inserting the casts required to pacify the linter which tends to degrade
the soundness and cosmetics of the code in question.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]