Re: Refactor compile-time assertion checks for C/C++ - Mailing list pgsql-hackers

From Michael Paquier
Subject Re: Refactor compile-time assertion checks for C/C++
Date
Msg-id 20200317041156.GA112208@paquier.xyz
Whole thread Raw
In response to Re: Refactor compile-time assertion checks for C/C++  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Refactor compile-time assertion checks for C/C++
List pgsql-hackers
On Mon, Mar 16, 2020 at 10:35:05PM -0400, Tom Lane wrote:
> Michael Paquier <michael@paquier.xyz> writes:
>> C++ does not allow defining a struct inside a sizeof() call, so in
>> this case StaticAssertExpr() does not work with the previous extension
>> in C++.  StaticAssertStmt() does the work though.
>
> [ scratches head... ]  A do{} is okay in an expression in C++ ??

cpp-fallback-fix.patch in [1] was doing that.

The fun does not stop here.  gcc is fine when using that for C and
C++:
#define StaticAssertStmt(condition, errmessage) \
   do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
#define StaticAssertExpr(condition, errmessage) \
   ((void) ({ StaticAssertStmt(condition, errmessage); }))

But then problems come from MSVC which does not like the do{} part for
statements, and this works:
#define StaticAssertStmt(condition, errmessage) \
    ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
#define StaticAssertExpr(condition, errmessage) \
    StaticAssertStmt(condition, errmessage)

[1]: https://postgr.es/m/20200313115033.GA183471@paquier.xyz
--
Michael

Attachment

pgsql-hackers by date:

Previous
From: Thunder
Date:
Subject: Re:Re: Re:Standby got fatal after the crash recovery
Next
From: Amit Kapila
Date:
Subject: Re: error context for vacuum to include block number