Portable StaticAssertExpr

Started by Peter Eisentraut11 days ago3 messages
Jump to latest
#1Peter Eisentraut
peter_e@gmx.net

The current implementation of StaticAssertExpr() uses the GCC extension
statement expressions.

In this patch, I'm proposing a different implementation that doesn't
require nonstandard extensions. The trick is to put the static_assert()
into a struct definition. This appears to be a common way to do this.
(See [0]https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3715.pdf for references.)

Unfortunately, MSVC before version 19.33 fails to compile this, but
since it was fixed later, I think this was just a bug that was later
fixed. I'm keeping the old workaround code as a fallback for this case,
but eventually we can remove that.

For C++, the struct trick doesn't work, but I found a different
implementation that works on all supported compilers, using lambda
expressions. I found this at [1]https://stackoverflow.com/questions/31311748.

[0]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3715.pdf
[1]: https://stackoverflow.com/questions/31311748

Attachments:

0001-Portable-StaticAssertExpr.patchtext/plain; charset=UTF-8; name=0001-Portable-StaticAssertExpr.patchDownload+22-70
#2Jelte Fennema-Nio
postgres@jeltef.nl
In reply to: Peter Eisentraut (#1)
Re: Portable StaticAssertExpr

On Mon, 23 Feb 2026 at 08:00, Peter Eisentraut <peter@eisentraut.org> wrote:

In this patch, I'm proposing a different implementation that doesn't
require nonstandard extensions.

I tried this out in one of my WIP patches for for better C++ and it
works correctly indeed.

Small nit, instead of nesting ifdefs. I think this looks a bit nicer:

# ifdef __cplusplus
...
#elif !defined(_MSC_VER) || _MSC_VER >= 1933
...
#else
...
#endif

#3Peter Eisentraut
peter_e@gmx.net
In reply to: Jelte Fennema-Nio (#2)
Re: Portable StaticAssertExpr

On 27.02.26 11:48, Jelte Fennema-Nio wrote:

On Mon, 23 Feb 2026 at 08:00, Peter Eisentraut <peter@eisentraut.org> wrote:

In this patch, I'm proposing a different implementation that doesn't
require nonstandard extensions.

I tried this out in one of my WIP patches for for better C++ and it
works correctly indeed.

Thanks, I have committed this.

Small nit, instead of nesting ifdefs. I think this looks a bit nicer:

# ifdef __cplusplus
...
#elif !defined(_MSC_VER) || _MSC_VER >= 1933
...
#else
...
#endif

I didn't change this, because I think it is better to keep the C code
first and then the C++ as an alternate variant last (which is also
existing style, at least in some parts), even if this requires some
additional logical negations.