[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: master 37889523278: Add new `swap` macro and use it
From: |
Stefan Kangas |
Subject: |
Re: master 37889523278: Add new `swap` macro and use it |
Date: |
Sat, 6 Jan 2024 00:50:55 -0800 |
Po Lu <luangruo@yahoo.com> writes:
> Stefan Kangas <stefankangas@gmail.com> writes:
>
>> branch: master
>> commit 37889523278fe65733938fb11c3701898309961c
>> Author: Stefan Kangas <stefankangas@gmail.com>
>> Commit: Stefan Kangas <stefankangas@gmail.com>
>>
>> Add new `swap` macro and use it
>>
>> A `swap` macro prevents programming errors and is more concise.
>> It is a natural addition to our existing `min` and `max` macros.
>
> It's well known that a generic "swap" macro is impossible to express in
> Standard C for lack of a standard "typeof"-like expression. To
> compensate for this limitation, the macro must accept an argument
> identifying the type of the field where the temporary value is saved,
> which is more trouble than it's worth and reintroduces the type
> conversion problems such macros are intended to prevent.
>
> Neither typeof nor __typeof__ are portable by our standards, the latter
> being an extension compatible with the Standard enabled when GCC is
> configured to obey the standard to the letter, while the former is a
> keyword enabled in other situations.
Ouch, right. I was inspired by some other large project, let's leave it
unnamed, but their C chops were big enough that I let my guard down.
Unfortunately for me, they happen to support far fewer compilers than we
do. Sorry about that, and thanks for being on your toes.
I had a look at what Gnulib has, and came up with the attached patch.
It still lets us avoid the type conversion problems on GCC, clang and
reasonably recent versions of IBM C and sunc. I'm not sure it's worth
it though, so perhaps my original patch should just be reverted.
I copied in Paul just in case, and I'm hoping Eli will chime in too.
portable-swap.diff
Description: Text Data
Re: master 37889523278: Add new `swap` macro and use it, Stefan Kangas, 2024/01/13
Re: master 37889523278: Add new `swap` macro and use it,
Stefan Kangas <=