Re: Refactor compile-time assertion checks for C/C++

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Georgios Kokolatos <gkokolatos(at)pm(dot)me>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Refactor compile-time assertion checks for C/C++
Date: 2020-03-12 07:12:32
Message-ID: 20200312071232.GA1739@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Mar 12, 2020 at 12:33:21AM -0400, Tom Lane wrote:
> I looked at this and tried it on an old (non HAVE__STATIC_ASSERT)
> gcc version. Seems to work, but I have a couple cosmetic suggestions:

Thanks for the review.

> 1. The comment block above this was never updated to mention that
> we're now catering for C++ too. Maybe something like
>
> + * On recent C++ compilers, we can use standard static_assert().
> + *

Sounds fine to me. Looking here this is present since GCC 4.3:
https://gcc.gnu.org/projects/cxx-status.html#cxx11

For MSVC, actually I was a bit wrong, only the flavor without error
message is supported since VS 2017, and the one we use is much older:
https://docs.microsoft.com/en-us/cpp/cpp/static-assert?view=vs-2015

So, should we add a reference about both in the new comment? I would
actually not add them, so I have used your suggestion in the attached,
but the comment block above does that for _Static_assert(). Do you
think it is better to add some references to some of those compilers
(say GCC 4.3, MSVC)? Just stick with your suggestion? Or stick with
your version and replace the reference to GCC 4.6 with something like
"recent compilers"?

> 2. I think you could simplify the #elif to just
>
> #elif defined(__cplusplus) && __cpp_static_assert >= 200410
>
> Per the C standard, an unrecognized identifier in an #if condition
> is replaced by zero. So the condition will come out false as desired
> if __cpp_static_assert isn't defined; you don't need to test that
> separately.

Thanks, indeed.
--
Michael

Attachment Content-Type Size
v3-0001-Refactor-assertion-definitions-in-c.h.patch text/x-diff 3.0 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2020-03-12 07:28:05 Re: Berserk Autovacuum (let's save next Mandrill)
Previous Message Jeff Davis 2020-03-12 06:55:35 Re: Memory-Bounded Hash Aggregation